/*
 *  Copyright (c) 2013 The CCP project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a Beijing Speedtong Information Technology Co.,Ltd license
 *  that can be found in the LICENSE file in the root of the web site.
 *
 *   http://www.yuntongxun.com
 *
 *  An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */
package com.yuntongxun.ecdemo.ui.chatting;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.AudioManager;
import android.media.ToneGenerator;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Vibrator;
import android.provider.MediaStore;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.Editable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.RequestParams;
import com.melink.bqmmsdk.widget.BQMMEditView;
import com.yuntongxun.ecdemo.R;
import com.yuntongxun.ecdemo.common.CCPAppManager;
import com.yuntongxun.ecdemo.common.dialog.ECAlertDialog;
import com.yuntongxun.ecdemo.common.dialog.ECListDialog;
import com.yuntongxun.ecdemo.common.dialog.ECProgressDialog;
import com.yuntongxun.ecdemo.common.utils.ClipboardUtils;
import com.yuntongxun.ecdemo.common.utils.DemoUtils;
import com.yuntongxun.ecdemo.common.utils.ECNotificationManager;
import com.yuntongxun.ecdemo.common.utils.ECPreferenceSettings;
import com.yuntongxun.ecdemo.common.utils.ECPreferences;
import com.yuntongxun.ecdemo.common.utils.EmoticonUtil;
import com.yuntongxun.ecdemo.common.utils.FileAccessor;
import com.yuntongxun.ecdemo.common.utils.FileUtils;
import com.yuntongxun.ecdemo.common.utils.LogUtil;
import com.yuntongxun.ecdemo.common.utils.MediaPlayTools;
import com.yuntongxun.ecdemo.common.utils.ToastUtil;
import com.yuntongxun.ecdemo.core.ClientUser;
import com.yuntongxun.ecdemo.core.ECAsyncTask;
import com.yuntongxun.ecdemo.photopicker.PhotoPickerActivity;
import com.yuntongxun.ecdemo.pojo.ImUserState;
import com.yuntongxun.ecdemo.storage.ContactSqlManager;
import com.yuntongxun.ecdemo.storage.ConversationSqlManager;
import com.yuntongxun.ecdemo.storage.GroupSqlManager;
import com.yuntongxun.ecdemo.storage.IMessageSqlManager;
import com.yuntongxun.ecdemo.storage.ImgInfoSqlManager;
import com.yuntongxun.ecdemo.ui.CCPFragment;
import com.yuntongxun.ecdemo.ui.LocationActivity;
import com.yuntongxun.ecdemo.ui.LocationInfo;
import com.yuntongxun.ecdemo.ui.SDKCoreHelper;
import com.yuntongxun.ecdemo.ui.chatting.base.ECPullDownView;
import com.yuntongxun.ecdemo.ui.chatting.base.OnListViewBottomListener;
import com.yuntongxun.ecdemo.ui.chatting.base.OnListViewTopListener;
import com.yuntongxun.ecdemo.ui.chatting.base.OnRefreshAdapterDataListener;
import com.yuntongxun.ecdemo.ui.chatting.model.ImgInfo;
import com.yuntongxun.ecdemo.ui.chatting.view.CCPChattingFooter2;
import com.yuntongxun.ecdemo.ui.contact.AtSomeoneUI;
import com.yuntongxun.ecdemo.ui.contact.ContactLogic;
import com.yuntongxun.ecdemo.ui.contact.ECContacts;
import com.yuntongxun.ecdemo.ui.group.GroupInfoActivity;
import com.yuntongxun.ecdemo.ui.meeting.MeetingHelper;
import com.yuntongxun.ecdemo.ui.meeting.VoiceMeetingActivity;
import com.yuntongxun.ecdemo.ui.plugin.FileExplorerActivity;
import com.yuntongxun.ecdemo.voicechat.api.ApiConstants;
import com.yuntongxun.ecdemo.voicechat.bean.Gift;
import com.yuntongxun.ecdemo.voicechat.bean.PostResult;
import com.yuntongxun.ecdemo.voicechat.bean.UserInfo;
import com.yuntongxun.ecdemo.voicechat.comman.EventBusTags;
import com.yuntongxun.ecdemo.voicechat.comman.GiftContacts;
import com.yuntongxun.ecdemo.voicechat.listeners.IGiftSelectItemListener;
import com.yuntongxun.ecdemo.voicechat.presenter.ICommonRequestPresenter;
import com.yuntongxun.ecdemo.voicechat.presenter.impl.CommonRequestPresenterImpl;
import com.yuntongxun.ecdemo.voicechat.ui.activity.OthersInfoActivity;
import com.yuntongxun.ecdemo.voicechat.ui.adapter.GiftsSystemAdapter;
import com.yuntongxun.ecdemo.voicechat.ui.services.UploadFileService;
import com.yuntongxun.ecdemo.voicechat.util.CommonUtils;
import com.yuntongxun.ecdemo.voicechat.util.JsonHelper;
import com.yuntongxun.ecdemo.voicechat.util.iview.ICommonViewUi;
import com.yuntongxun.ecdemo.voicechat.util.recoder.AudioRecoderUtils;
import com.yuntongxun.ecdemo.voicechat.view.headerfooterrecyclerview.ExStaggeredGridLayoutManager;
import com.yuntongxun.ecdemo.voicechat.view.headerfooterrecyclerview.HeaderAndFooterRecyclerViewAdapter;
import com.yuntongxun.ecdemo.voicechat.view.headerfooterrecyclerview.HeaderSpanSizeLookup;
import com.yuntongxun.ecsdk.ECChatManager;
import com.yuntongxun.ecsdk.ECDevice;
import com.yuntongxun.ecsdk.ECError;
import com.yuntongxun.ecsdk.ECGroupManager;
import com.yuntongxun.ecsdk.ECMeetingManager;
import com.yuntongxun.ecsdk.ECMessage;
import com.yuntongxun.ecsdk.ECVoIPCallManager.CallType;
import com.yuntongxun.ecsdk.SdkErrorCode;
import com.yuntongxun.ecsdk.im.ECFileMessageBody;
import com.yuntongxun.ecsdk.im.ECGroup;
import com.yuntongxun.ecsdk.im.ECGroupMember;
import com.yuntongxun.ecsdk.im.ECImageMessageBody;
import com.yuntongxun.ecsdk.im.ECLocationMessageBody;
import com.yuntongxun.ecsdk.im.ECTextMessageBody;
import com.yuntongxun.ecsdk.im.ECUserStateMessageBody;
import com.yuntongxun.ecsdk.im.ECVoiceMessageBody;
import com.yuntongxun.ecsdk.meeting.ECMeeting;
import com.yuntongxun.ecsdk.platformtools.ECHandlerHelper;
import com.yunzhanghu.redpacketsdk.bean.RPUserBean;
import com.yunzhanghu.redpacketui.callback.GroupMemberCallback;
import com.yunzhanghu.redpacketui.callback.NotifyGroupMemberCallback;
import com.yunzhanghu.redpacketui.utils.RPGroupMemberUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.InvalidClassException;
import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;
import utils.RedPacketConstant;
import utils.RedPacketUtil;

/**
 * com.yuntongxun.ecdemo.ui.chatting in ECDemo_Android
 * Created by Jorstin on 2015/6/16.
 */
public class ChattingFragment extends CCPFragment implements
        View.OnClickListener, AbsListView.OnScrollListener,
        IMChattingHelper.OnMessageReportCallback,
        CustomerServiceHelper.OnCustomerServiceListener, IGiftSelectItemListener, ICommonViewUi {

    AudioRecoderUtils recorderUtils = new AudioRecoderUtils();
    File audioFile;

    public static final String TAG = "ECSDK_Demo.ChattingFragment";
    private static final int WHAT_ON_COMPUTATION_TIME = 10000;
    /**
     * request code for tack pic
     */
    public static final int REQUEST_CODE_TAKE_PICTURE = 0x3;
    public static final int REQUEST_CODE_TAKE_LOCATION = 0x11;
    public static final int REQUEST_CODE_LOAD_IMAGE = 0x4;
    public static final int REQUEST_CODE_IMAGE_CROP = 0x5;
    /**
     * 查看名片
     */
    public static final int REQUEST_VIEW_CARD = 0x6;
    /**
     * 选择回复联系人
     */
    public static final int SELECT_AT_SOMONE = 0xD4;

    /**
     * 联系人账号
     */
    public final static String RECIPIENTS = "recipients";
    /**
     * 联系人名称
     */
    public final static String CONTACT_USER = "contact_user";
    public final static String FROM_CHATTING_ACTIVITY = "from_chatting_activity";
    public final static String CUSTOMER_SERVICE = "is_customer_service";

    /**
     * 按键振动时长
     */
    public static final int TONE_LENGTH_MS = 200;
    /**
     * 音量值
     */
    private static final float TONE_RELATIVE_VOLUME = 100.0F;
    /**
     * 待发送的语音文件最短时长
     */
    private static final int MIN_TIME = 500;
    /**
     * 聊天界面消息适配器
     */
    private ChattingListAdapter2 mChattingAdapter;
    /** 界面消息下拉刷新 */
    // private RefreshableView mPullDownView;
    // private long mPageCount;
    /**
     * 历史聊天纪录消息显示View
     */
    private ListView mListView;
    private View mListViewHeadView;
    /**
     * 聊天界面附加聊天控件面板
     */
    private CCPChattingFooter2 mChattingFooter;
    /**
     * 选择图片拍照路径
     */
    private String mFilePath;
    /**
     * 会话ID
     */
    private long mThread = -1;
    /**
     * 会话联系人账号
     */
    private String mRecipients;
    /**
     * 联系人名称
     */
    private String mUsername;
    /**
     * 计算当前录音时长
     */
    private long computationTime = -1L;
    /**
     * 当前语言录制文件的时间长度
     */
    private int mVoiceRecodeTime = 0;
    /**
     * 是否使用边录制便传送模式发送语音
     */
    private boolean isRecordAndSend = false;

    private ToneGenerator mToneGenerator;
    /**
     * 录音剩余时间Toast提示
     */
    private Toast mRecordTipsToast;
    private ECHandlerHelper mHandlerHelper = new ECHandlerHelper();
    private Handler mHandler = new Handler(Looper.getMainLooper());
    private Handler mVoiceHandler;
    private Looper mChattingLooper;
    /**
     * IM聊天管理工具
     */
    private ECChatManager mChatManager;
    /**
     * 聊天底部导航控件通知回调
     */
    private OnChattingFooterImpl mChattingFooterImpl = new OnChattingFooterImpl(
            (ChattingActivity) getActivity());
    /**
     * 聊天功能插件接口实现
     */
    private OnOnChattingPanelImpl mChattingPanelImpl = new OnOnChattingPanelImpl();
    private ECPullDownView mECPullDownView;
    /**
     * 是否查看消息模式
     */
    private boolean isViewMode = false;
    private View mMsgLayoutMask;
    public boolean mAtsomeone = false;
    /**
     * 在线客服
     */
    private boolean mCustomerService = false;
    private OnChattingAttachListener mAttachListener;

    /**
     * 选择礼物布局
     */
    private RelativeLayout mGiftsLayout;
    private LinearLayout mGiftsTopLayout;
    public RecyclerView mSystemGiftRv;
    public TextView mNoGiftTipTv;
    public LinearLayout mSystemGiftSelectBtn;
    public LinearLayout mReceiverGiftSelectBtn;
    public CardView mCorfirmGiftBtn;
    public int mGiftPosition = -1;

    public TextView addMeetingBtn;

    private HeaderAndFooterRecyclerViewAdapter adapter;
    private ExStaggeredGridLayoutManager staggeredGridLayoutManager;
    private List<Gift> mResultList = new ArrayList<Gift>();

    private int mSystemCurrentPage = 1;//系统礼物当前页面
    private int mReceiverCurrentPage = 1;//收到礼物当前页面

    private ICommonRequestPresenter iCommonRequestPresenter;

    public static final String CUSTOMER_LITTLE_INFO = "customer_info";

    @Override
    protected int getLayoutId() {
        return R.layout.chatting_activity;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mAttachListener = (OnChattingAttachListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnChattingAttachListener");
        }
    }

    private ChattingActivity getChattingActivity() {
        if (getActivity() instanceof ChattingActivity) {
            return (ChattingActivity) getActivity();
        }
        throw new RuntimeException(getActivity().toString()
                + " must implement OnChattingAttachListener");
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 初始化联系人信息
        initActivityState(savedInstanceState);

        EventBus.getDefault().register(this);

    }

    @Override
    public final View onCreateView(LayoutInflater inflater,
                                   ViewGroup container, Bundle savedInstanceState) {
        View contentView = super.onCreateView(inflater, container,
                savedInstanceState);
        ViewGroup.LayoutParams layoutParams = contentView.getLayoutParams();
        if (layoutParams == null) {
            layoutParams = new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT);
        }
        contentView.setLayoutParams(layoutParams);
        return contentView;
    }


    String cid;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        LogUtil.d(TAG, "onActivityCreated");
        super.onActivityCreated(savedInstanceState);

        Bundle bundle = getArguments();
        if (bundle != null)
            cid = bundle.getString(CUSTOMER_LITTLE_INFO);

        // 初始化界面资源
        initView();

        queryUIMessage();

        // 初始化IM聊天工具API
        mChatManager = SDKCoreHelper.getECChatManager();
        HandlerThread thread = new HandlerThread("ChattingVoiceRecord",
                android.os.Process.THREAD_PRIORITY_BACKGROUND);
        thread.start();

        // Get the HandlerThread's Looper and use it for our Handler
        mChattingLooper = thread.getLooper();
        mVoiceHandler = new Handler(mChattingLooper);
        mVoiceHandler.post(new Runnable() {

            @Override
            public void run() {
                doEmojiPanel();
            }
        });

        registerReceiver(new String[]{IMessageSqlManager.ACTION_GROUP_DEL, IMChattingHelper.INTENT_ACTION_CHAT_USER_STATE, IMChattingHelper.INTENT_ACTION_CHAT_EDITTEXT_FOUCU});
    }

    private void queryUIMessage() {
        mListView.post(new Runnable() {

            @Override
            public void run() {
                if (mChattingAdapter.getCount() < 18) {
                    mECPullDownView.setIsCloseTopAllowRefersh(true);
                    mECPullDownView.setTopViewInitialize(false);
                }
                mListView.clearFocus();
                mChattingAdapter.notifyChange();
                mListView.setSelection(mChattingAdapter.getCount());
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mChattingAdapter!=null){
            mChattingAdapter.sopMediaPlayer();
        }
        EventBus.getDefault().unregister(this);

        AppPanelControl.setShowVoipCall(true);
        if (mCustomerService) {
            CustomerServiceHelper.addCustomerServiceListener(null);
            CustomerServiceHelper.finishService(mRecipients);
        }
        if (mChattingLooper != null) {
            mChattingLooper.quit();
            mChattingLooper = null;
        }
        if (mChattingFooter != null) {
            mChattingFooter.onDestory();
            mChattingFooter = null;
        }

        if (mHandlerHelper != null) {
            mHandlerHelper.getTheadHandler().removeCallbacksAndMessages(null);
            mHandlerHelper = null;
        }
        if (mVoiceHandler != null) {
            mVoiceHandler.removeCallbacksAndMessages(null);
            mVoiceHandler = null;
        }
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        if (mListView != null) {
            mListView.setOnItemLongClickListener(null);
            mListView.setOnItemClickListener(null);
        }
        if (mChattingAdapter != null) {
            mChattingAdapter.onDestroy();
            mListView.setAdapter(null);
        }
        mChatManager = null;
        mOnItemLongClickListener = null;
        mOnListViewBottomListener = null;
        mOnListViewTopListener = null;
        mOnRefreshAdapterDataListener = null;
        if (mChattingFooterImpl != null) {
            mChattingFooterImpl.release();
            mChattingFooterImpl = null;
        }
        mChattingPanelImpl = null;
        mECPullDownView = null;
        setChattingContactId("");
        IMChattingHelper.setOnMessageReportCallback(null);
        System.gc();


    }

    /**
     * 初始化聊天界面资源
     */
    private void initView() {
        mListView = (ListView) findViewById(R.id.chatting_history_lv);
        mListView.setTranscriptMode(ListView.TRANSCRIPT_MODE_NORMAL);
        mListView.setItemsCanFocus(false);
        mListView.setOnScrollListener(this);
        mListView.setKeepScreenOn(false);
        mListView.setStackFromBottom(false);
        mListView.setFocusable(false);
        mListView.setFocusableInTouchMode(false);
        mListView.setOnItemLongClickListener(mOnItemLongClickListener);


        registerForContextMenu(mListView);

        mListViewHeadView = getChattingActivity().getLayoutInflater().inflate(
                R.layout.chatting_list_header, null);
        mListView.addHeaderView(mListViewHeadView);
        mListView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                hideSoftKeyboard();
                if (mChattingFooter != null) {
                    // After the input method you can use the record button.
                    mChattingFooter.hideBottomPanel();
                }
                return false;
            }
        });

        mMsgLayoutMask = findViewById(R.id.message_layout_mask);
        mMsgLayoutMask.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                hideMsgLayoutMask();
                mListView.setSelection(mListView.getCount() - 1);
                return true;
            }
        });
        /************************************************************************************************************/
        mECPullDownView = (ECPullDownView) findViewById(R.id.chatting_pull_down_view);
        mECPullDownView.setTopViewInitialize(true);
        mECPullDownView.setIsCloseTopAllowRefersh(false);
        mECPullDownView.setHasbottomViewWithoutscroll(false);
        mECPullDownView
                .setOnRefreshAdapterDataListener(mOnRefreshAdapterDataListener);
        mECPullDownView.setOnListViewTopListener(mOnListViewTopListener);
        mECPullDownView.setOnListViewBottomListener(mOnListViewBottomListener);

        mChattingFooter = (CCPChattingFooter2) findViewById(R.id.nav_footer);

        mChattingFooter.setOnRecordClickListener(new CCPChattingFooter2.OnRecordClickListener() {
            @Override
            public void onRecordClick() {
                if (mChattingAdapter!=null){
                    mChattingAdapter.sopMediaPlayer();
                }

            }
        });
        addMeetingBtn = (TextView) findViewById(R.id.add_meeting_tv);

        addMeetingBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ecMeeting == null)
                    return;
                doStartMeetingActivity(ecMeeting, null, true);
                sendMessageAddMeeting("Join group chat");
            }
        });

        // 注册聊天面板状态回调通知、包含录音按钮按钮下放开等录音操作
        mChattingFooter.setOnChattingFooterLinstener(mChattingFooterImpl);
        mChattingFooter.setCustomerId(cid);
        // 注册聊天面板附加功能（图片、拍照、文件）被点击回调通知
        mChattingFooter.setOnChattingPanelClickListener(mChattingPanelImpl);
        // 注册一个聊天面板文本输入框改变监听
        mChattingFooter.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                LogUtil.d(TAG, "[onTextChanged]");
                String text = String.valueOf(s);
                String value = text.substring(start, start + count);
                if (("@".equals(value) && isPeerChat()) && !text.equals(mChattingFooter.getLastContent()) && !mChattingFooter.isSetAtSomeoneing()) {
                    mChattingFooter.setLastContent(text);
                    mChattingFooter.setInsertPos(start + 1);
                    boolean handler = (text == null || start < 0 || text.length() < start);
                    if (!handler) {
                        Intent action = new Intent();
                        action.setClass(getChattingActivity(), AtSomeoneUI.class);
                        action.putExtra(AtSomeoneUI.EXTRA_GROUP_ID, mRecipients);
                        action.putExtra(AtSomeoneUI.EXTRA_CHAT_USER, CCPAppManager.getClientUser().getUserId());
                        startActivityForResult(action, 212);
                    }
                    return;
                } else if (!text.equals(mChattingFooter.getLastContent())) {
                    mChattingFooter.setLastContent(text);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        if (isPeerChat()) {
            mChattingFooter.mAppPanel.mAppPanelControl.capVoiceChat = new int[]{R.string.app_panel_1,
                    R.string.app_panel_2, R.string.app_panel_3, R.string.app_panel_5};
            mChattingFooter.invalidate();
        }

        mChattingAdapter = new ChattingListAdapter2(getActivity(), ECMessage.createECMessage(ECMessage.Type.NONE), mRecipients, mThread);
        mListView.setAdapter(mChattingAdapter);

        iCommonRequestPresenter = new CommonRequestPresenterImpl(getContext(), this);

        //选择礼物布局
        mGiftsLayout = (RelativeLayout) findViewById(R.id.gifts_layout_mask);
        mGiftsTopLayout = (LinearLayout) findViewById(R.id.gifts_top_layout);
        mSystemGiftRv = (RecyclerView) findViewById(R.id.system_gifts_recyclerview);
        mNoGiftTipTv = (TextView) findViewById(R.id.gifts_tips_tv);
        mSystemGiftSelectBtn = (LinearLayout) findViewById(R.id.system_gifts_btn);
        mReceiverGiftSelectBtn = (LinearLayout) findViewById(R.id.recieve_gifts_btn);
        mCorfirmGiftBtn = (CardView) findViewById(R.id.confirm_select_gift_btn);

        mGiftsTopLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mGiftsLayout.setVisibility(View.GONE);
            }
        });

        mSystemGiftSelectBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                toRequest(ApiConstants.EventTags.GIFT_SYSTEM_LIST_REFRESH);

                selectSystemOrReceiverGift(mSystemGiftSelectBtn);
            }
        });

        mReceiverGiftSelectBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                toRequest(ApiConstants.EventTags.GIFT_RECEIVER_LIST_REFRESH);

                selectSystemOrReceiverGift(mReceiverGiftSelectBtn);
            }
        });

        mCorfirmGiftBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int selectPosition = -1;
                for (int i = 0; i < mResultList.size(); i++) {
                    if (mResultList.get(i).isSelect()) {
                        selectPosition = i;
                    }
                }
                if (selectPosition == -1) {
                    Toast.makeText(getContext(), "please select gift before", Toast.LENGTH_SHORT).show();
                } else {
                    toRequest(ApiConstants.EventTags.ACCOUNT_INFO);
                }
            }
        });

        refreshListView();

    }

    ECMeeting ecMeeting;

    public void setEcMeeting(ECMeeting ecMeeting) {
        this.ecMeeting = ecMeeting;
    }

    public void selectSystemOrReceiverGift(LinearLayout giftLayout) {

        mSystemGiftSelectBtn.setBackgroundColor(getResources().getColor(R.color.white));
        mReceiverGiftSelectBtn.setBackgroundColor(getResources().getColor(R.color.white));

        giftLayout.setBackgroundColor(getResources().getColor(R.color.bg_gray));
    }

    public void refreshListView() {

        GiftsSystemAdapter mIntermediary = new GiftsSystemAdapter(getContext(), mResultList, this);

        adapter = new HeaderAndFooterRecyclerViewAdapter(mIntermediary);

        mSystemGiftRv.setAdapter(adapter);

        staggeredGridLayoutManager = new ExStaggeredGridLayoutManager(1, StaggeredGridLayoutManager.HORIZONTAL);

        staggeredGridLayoutManager.setSpanSizeLookup(new HeaderSpanSizeLookup((HeaderAndFooterRecyclerViewAdapter) mSystemGiftRv.getAdapter(), staggeredGridLayoutManager.getSpanCount()));

        mSystemGiftRv.setLayoutManager(staggeredGridLayoutManager);

        if (mResultList.size() == 0) {
            mNoGiftTipTv.setVisibility(View.VISIBLE);
            mNoGiftTipTv.setText("no gifts");
        } else {
            mNoGiftTipTv.setVisibility(View.GONE);
        }
    }

    @Override
    public void onItemClick(int position) {

        mGiftPosition = position;

        clearAllSelect();

        Gift gift = mResultList.get(position);
        gift.setSelect(true);

        adapter.notifyDataSetChanged();

    }

    /**
     * 清空掉所有选择
     */
    private void clearAllSelect() {

        for (Gift gift : mResultList) {
            gift.setSelect(false);
        }
    }

    private void hideBottom() {
        // 隐藏键盘
        hideSoftKeyboard();
        if (mChattingFooter != null) {
            // 隐藏更多的聊天功能面板
            mChattingFooter.hideBottomPanel();
        }
    }

    private Animation mAnimation;

    private void showMsgLayoutMask() {
        if (isViewMode && !mMsgLayoutMask.isShown()) {
            if (mAnimation == null) {
                mAnimation = AnimationUtils.loadAnimation(getChattingActivity(), R.anim.buttomtip_in);
            }
            mMsgLayoutMask.setVisibility(View.VISIBLE);
            mMsgLayoutMask.startAnimation(mAnimation);
            mAnimation.start();
        }
    }

    private void hideMsgLayoutMask() {
        if (mMsgLayoutMask != null && mMsgLayoutMask.isShown()) {
            mMsgLayoutMask.setVisibility(View.GONE);
        }
    }

    /**
     * 读取聊天界面联系人会话参数信息
     *
     * @param savedInstanceState
     */
    private void initActivityState(Bundle savedInstanceState) {
        Intent intent = getActivity().getIntent();
        mRecipients = intent.getStringExtra(RECIPIENTS);
        mUsername = intent.getStringExtra(CONTACT_USER);

        Log.e("result", "mRecipients>" + mRecipients + "; mUsername>" + mUsername);

        if (TextUtils.isEmpty(mRecipients)) {
            ToastUtil.showMessage("联系人账号不存在");
            finish();
            return;
        }
        mCustomerService = ContactLogic.isCustomService(mRecipients);

        if (mUsername == null) {
            ECContacts contact = ContactSqlManager.getContact(mRecipients);
            if (contact != null) {
                mUsername = contact.getNickname();
            } else {
                mUsername = mRecipients;
            }
        }
        if (!isPeerChat()) {
            IMessageSqlManager.checkContact(mRecipients, mUsername);
        }
        getTopBarView().setTopBarToStatus(1, R.drawable.ic_arrow_back, isPeerChat() ? R.drawable.actionbar_facefriend_icon : R.drawable.actionbar_particular_icon, mUsername, this);
        setActionBarTitle(mUsername);
        mThread = ConversationSqlManager.querySessionIdForBySessionId(mRecipients);
    }

    /**
     * 是否群组
     *
     * @return
     */
    public boolean isPeerChat() {
        return mRecipients != null && mRecipients.toLowerCase().startsWith("g");
    }

    /**
     * 发送信息
     *
     * @param message
     */
    public void sendMessageAddMeeting(String message) {
        mChattingFooterImpl.OnSendTextMessageRequest(message);
    }

    /**
     * 返回聊天消息适配器
     *
     * @return the mChattingAdapter
     */
    public ChattingListAdapter2 getChattingAdapter() {
        return mChattingAdapter;
    }

    public CCPChattingFooter2 getChattingFooter() {
        return mChattingFooter;
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
            View topView = mListView.getChildAt(mListView.getFirstVisiblePosition());
            if ((topView != null) && (topView.getTop() == 0)) {
                LogUtil.d(LogUtil.getLogUtilsTag(ChattingActivity.class), "doLoadingView auto pull");
                mECPullDownView.startTopScroll();
            }
        }
    }


    private boolean mHandlerDelChar = false;
    private String fileName;
    private String filePath;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            LogUtil.d(TAG, "keycode back , chatfooter mode: " + mChattingFooter.getMode());
            if (!mChattingFooter.isButtomPanelNotVisibility()) {
                hideBottom();
                return true;
            }
            setIsFinish(true);
        } else if (keyCode == KeyEvent.KEYCODE_DEL) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                mHandlerDelChar = !(mChattingFooter.getCharAtCursor() != (char) (8197));
            }
            if (event.getAction() == KeyEvent.ACTION_UP && mHandlerDelChar) {
                mHandlerDelChar = false;
                CCPChattingFooter2 footer = this.mChattingFooter;
                int selectionStart = footer.getSelectionStart();
                String text = footer.getLastText().substring(0, selectionStart);
                int atIndex = text.lastIndexOf('@');
                if (atIndex < text.length() && atIndex >= 0) {
                    delAtSomeBody(text.substring(atIndex, selectionStart));
                    String subStartText = text.substring(0, atIndex);
                    String subSecondText = footer.getLastText().substring(selectionStart);
                    StringBuilder sb = new StringBuilder();
                    sb.append(subStartText).append(subSecondText);
                    footer.setLastText(sb.toString());
                    footer.mEditText.setSelection(atIndex);
                }
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    private void delAtSomeBody(String text) {
        if (TextUtils.isEmpty(text)) {
            return;
        }

        this.mChattingFooter.delSomeBody(text.replace("@", "").replace((char) (8197), ' ').trim());
    }

    @Override  //控制是否能刷新数据top
    public void onScroll(AbsListView view, int firstVisibleItem,
                         int visibleItemCount, int totalItemCount) {

        LogUtil.d(TAG, "[onScroll] firstVisibleItem :" + firstVisibleItem + " ,visibleItemCount:" + visibleItemCount + " ,totalItemCount:" + totalItemCount);
        isViewMode = !((firstVisibleItem + visibleItemCount) == totalItemCount);
        if (mECPullDownView != null) {
            if (!mChattingAdapter.isLimitCount()) {
                mECPullDownView.setIsCloseTopAllowRefersh(false);
            } else {
                mECPullDownView.setIsCloseTopAllowRefersh(true);//小于18
            }
        }
        if (!isViewMode) hideMsgLayoutMask();
    }

    @Override
    public void onResume() {
        super.onResume();

        IMChattingHelper.setOnMessageReportCallback(this);
        if (mCustomerService) {
            CustomerServiceHelper.addCustomerServiceListener(this);
        }
        // 将所有的未读消息设置为已读
        setChattingSessionRead();
        mChattingAdapter.onResume();
        try {
            ECPreferences.savePreference(ECPreferenceSettings.SETTINGS_AT, "", true);
        } catch (InvalidClassException e) {
        }
        checkPreviewImage();
        setChattingContactId(mRecipients);
        ECNotificationManager.getInstance().forceCancelNotification();

        initSettings(mRecipients);
        if (isPeerChat() && !GroupSqlManager.getJoinState(mRecipients)) {
            getTopBarView().setTopBarToStatus(1, R.drawable.ic_arrow_back, -1, mUsername, this);
            mChattingFooter.setVisibility(View.GONE);
            return;
        }
        mChattingFooter.setVisibility(View.VISIBLE);
    }

    //广播接收
    public void onEvent(PostResult postResult) {

        switch (postResult.getTag()) {
            case EventBusTags.TOPIC_KEY:
                //话题接收
                String topicMessage = (String) postResult.getResult();
                handlesendTopicMessage(topicMessage);
                break;
        }

    }

    /**
     * @param mRecipients
     */
    private void initSettings(String mRecipients) {
        if (isPeerChat()) {
            ECGroup ecGroup = GroupSqlManager.getECGroup(mRecipients);
            if (ecGroup != null) {
                setActionBarTitle(ecGroup.getName() != null ? ecGroup.getName() : ecGroup.getGroupId());
                SpannableString charSequence = setNewMessageMute(!ecGroup.isNotice());
                if (charSequence != null) {
                    getTopBarView().setTitle(charSequence);
                }
            }else{
                finish();
                ToastUtil.showMessage("Do not exists");
            }
            ECGroupManager groupManager = ECDevice.getECGroupManager();
            // 调用获取群组成员接口，设置结果回调
            groupManager.queryGroupMembers(ecGroup.getGroupId(),
                    new ECGroupManager.OnQueryGroupMembersListener() {
                        @Override
                        public void onQueryGroupMembersComplete(ECError error
                                , final List members) {
                            if (error.errorCode == SdkErrorCode.REQUEST_SUCCESS
                                    && members != null) {
                                // 获取群组成员成功
                                // 将群组成员信息更新到本地缓存中（sqlite） 通知UI更新

                                RPGroupMemberUtil.getInstance().setGroupMemberListener(new NotifyGroupMemberCallback() {
                                    @Override
                                    public void getGroupMember(final String groupID, final GroupMemberCallback mCallBack) {

                                        List<RPUserBean> userBeanList = new ArrayList<RPUserBean>();

                                        for (int i = 0; i < members.size(); i++) {
                                            RPUserBean userBean = new RPUserBean();
                                            ECGroupMember member = (ECGroupMember) members.get(i);
                                            userBean.userId = member.getVoipAccount();
                                            if (userBean.userId.equals(CCPAppManager.getUserId())) {
                                                continue;
                                            }

                                            if (member != null) {
                                                userBean.userAvatar = "none";
                                                userBean.userNickname = TextUtils.isEmpty(member.getDisplayName()) ? member.getVoipAccount() : member.getDisplayName();
                                            } else {
                                                userBean.userNickname = userBean.userId;
                                                userBean.userAvatar = "none";
                                            }
                                            userBeanList.add(userBean);
                                        }
                                        mCallBack.setGroupMember(userBeanList);
                                    }
                                });

                                return;
                            }
                            // 群组成员获取失败
                            Log.e("ECSDK_Demo", "sync group detail fail " +
                                    ", errorCode=" + error.errorCode);
                        }

                    }
            );
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        handleSendUserStateMessage("0");
        stopPlayVoice();
        setChattingContactId("");

    }

    /**
     * 保存当前的聊天界面所对应的联系人、方便来消息屏蔽通知
     */
    private void setChattingContactId(String contactid) {
        try {
            ECPreferences.savePreference(ECPreferenceSettings.SETTING_CHATTING_CONTACTID, contactid, true);
        } catch (InvalidClassException e) {
            e.printStackTrace();
        }
    }

    /**
     * 检查是否有预览带发送图片
     */
    private void checkPreviewImage() {
        if (TextUtils.isEmpty(mFilePath)) {
            return;
        }
        boolean previewImage = ECPreferences.getSharedPreferences().getBoolean(ECPreferenceSettings.SETTINGS_PREVIEW_SELECTED.getId()
                , (Boolean) ECPreferenceSettings.SETTINGS_PREVIEW_SELECTED.getDefaultValue());
        if (previewImage) {
            try {
                ECPreferences.savePreference(ECPreferenceSettings.SETTINGS_PREVIEW_SELECTED, Boolean.FALSE, true);
                new ChattingAsyncTask(getChattingActivity()).execute(mFilePath);
                mFilePath = null;
            } catch (InvalidClassException e) {
                e.printStackTrace();
            }
        }
    }

    public long getmThread() {
        return mThread;
    }

    private void doEmojiPanel() {
        if (EmoticonUtil.getEmojiSize() == 0) {
            EmoticonUtil.initEmoji();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        LogUtil.d(TAG, "onActivityResult: requestCode=" + requestCode
                + ", resultCode=" + resultCode + ", data=" + data);

        // If there's no data (because the user didn't select a picture and
        // just hit BACK, for example), there's nothing to do.

        if (requestCode == 0x2a || requestCode == SELECT_AT_SOMONE) {
            if (data == null) {
                return;
            }
        } else if (resultCode != ChattingActivity.RESULT_OK) {
            LogUtil.d("onActivityResult: bail due to resultCode=" + resultCode);
            isFireMsg = false;
            return;
        }

        if (data != null && 0x2a == requestCode) {
            handleAttachUrl(data.getStringExtra("choosed_file_path"));
            return;
        }

        if (requestCode == REQUEST_CODE_TAKE_PICTURE
                || requestCode == REQUEST_CODE_LOAD_IMAGE) {
            if (requestCode == REQUEST_CODE_LOAD_IMAGE) {
                ArrayList<String> result = data.getStringArrayListExtra(PhotoPickerActivity.KEY_RESULT);
                if (result != null && !result.isEmpty()) {
                    mFilePath = result.get(0);
                } else {
                    mFilePath = DemoUtils.resolvePhotoFromIntent(
                            this.getActivity(), data,
                            FileAccessor.IMESSAGE_IMAGE);
                }
            }
            if (TextUtils.isEmpty(mFilePath)) {
                return;
            }
            File file = new File(mFilePath);
            if (file == null || !file.exists()) {

                return;
            }
            try {
                ECPreferences.savePreference(ECPreferenceSettings.SETTINGS_CROPIMAGE_OUTPUTPATH, file.getAbsolutePath(), true);
                Intent intent = new Intent(getChattingActivity(), ImagePreviewActivity.class);
                startActivityForResult(intent, REQUEST_CODE_IMAGE_CROP);
            } catch (InvalidClassException e1) {
                e1.printStackTrace();
            }
            return;
        }
        if (requestCode == REQUEST_VIEW_CARD && data != null) {
            boolean exit = data.getBooleanExtra(GroupInfoActivity.EXTRA_QUEIT, false);
            if (exit) {
                finish();
                return;
            }
            boolean reload = data.getBooleanExtra(GroupInfoActivity.EXTRA_RELOAD, false);
            if (reload) {
                mThread = mChattingAdapter.setUsername(mRecipients);
                queryUIMessage();
            }
        }

        if (requestCode == SELECT_AT_SOMONE) {
            String selectUser = data
                    .getStringExtra(AtSomeoneUI.EXTRA_SELECT_CONV_USER);
            if (TextUtils.isEmpty(selectUser)) {
                mChattingFooter.setAtSomebody("");
                LogUtil.d(TAG, "@ [nobody]");
                return;
            }
            LogUtil.d(TAG, "@ " + selectUser);
            ECContacts contact = ContactSqlManager.getContact(selectUser);
            if (contact == null) {
                return;
            }
            if (TextUtils.isEmpty(contact.getNickname())) {
                contact.setNickname(contact.getContactid());
            }
            mChattingFooter.setAtSomebody(contact.getNickname());
            mChattingFooter.putSomebody(contact);
            postSetAtSome();
            return;
        }

        if (requestCode == REQUEST_CODE_TAKE_LOCATION) {

            locationInfo = (LocationInfo) data.getSerializableExtra("location");

            handleSendLocationMessage(locationInfo);
        }

        if (requestCode == REQUEST_CODE_REDPACKET) {
            if (data != null) {
                handlesendRedPacketMessage(data);
            }
        }

    }

    private LocationInfo locationInfo;

    /**
     * 处理@某人
     */
    private void postSetAtSome() {
        String atSomebody = mChattingFooter.getAtSomebody();
        if (!TextUtils.isEmpty(atSomebody)) {
            String text = mChattingFooter.getLastText();
            int someInsertPosition = mChattingFooter.getInsertPos();
            if (someInsertPosition > text.length()) {
                someInsertPosition = text.length();
            }
            String message = text.substring(0, someInsertPosition) + atSomebody
                    + (char) (8197)
                    + text.substring(someInsertPosition, text.length());
            int selectoin = 1 + someInsertPosition + atSomebody.length();
            mChattingFooter.setLastContent(message);
            mChattingFooter.setLastText(message, selectoin, false);
            mChattingFooter.setLastContent(null);
            toggleSoftInput();
        }
    }

    /**
     * 处理附件
     *
     * @param path
     */
    private void handleAttachUrl(final String path) {
        File file = new File(path);
        if (!file.exists()) {
            return;
        }
        final long length = file.length();
        if (length > (10 * 1048576.0F)) {
            ToastUtil.showMessage("文件大小超过限制，最大不能超过10M");
            return;
        }
        ECAlertDialog buildAlert = ECAlertDialog.buildAlert(getActivity(), getString(R.string.plugin_upload_attach_size_tip, FileUtils.formatFileLength(length)), new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                handleSendFileAttachMessage(length, path);
            }
        });

        buildAlert.setTitle(R.string.app_tip);
        buildAlert.show();
    }

    /**
     * 处理文本发送方法事件通知
     *
     * @param text
     */
    private void handleSendTextMessage(CharSequence text) {
        if (text == null) {
            return;
        }
        if (text.toString().trim().length() <= 0) {
            canotSendEmptyMessage();
            return;
        }
        // 组建一个待发送的ECMessage
        ECMessage msg = ECMessage.createECMessage(ECMessage.Type.TXT);
        // 设置消息接收者
        msg.setTo(mRecipients);
        ECTextMessageBody msgBody = null;
        Boolean isBQMMMessage = false;
        String emojiNames = null;
        if (text.toString().contains(CCPChattingFooter2.TXT_MSGTYPE) && text.toString().contains(CCPChattingFooter2.MSG_DATA)) {
            try {
                JSONObject jsonObject = new JSONObject(text.toString());
                String emojiType = jsonObject.getString(CCPChattingFooter2.TXT_MSGTYPE);
                if (emojiType.equals(CCPChattingFooter2.EMOJITYPE) || emojiType.equals(CCPChattingFooter2.FACETYPE)) {//说明是含有BQMM的表情
                    isBQMMMessage = true;
                    emojiNames = jsonObject.getString(CCPChattingFooter2.EMOJI_TEXT);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        if (isBQMMMessage) {
            msgBody = new ECTextMessageBody(emojiNames);
            msg.setBody(msgBody);
            msg.setUserData(text.toString());
        } else {
            // 创建一个文本消息体，并添加到消息对象中
            msgBody = new ECTextMessageBody(text.toString());
            msg.setBody(msgBody);
        }

        String[] at = mChattingFooter.getAtSomeBody();
        msgBody.setAtMembers(at);
        mChattingFooter.clearSomeBody();
        try {
            // 发送消息，该函数见上
            long rowId = -1;
            if (mCustomerService) {
                rowId = CustomerServiceHelper.sendMCMessage(msg);
            } else {
                rowId = IMChattingHelper.sendECMessage(msg);
            }
            // 通知列表刷新
            msg.setId(rowId);
            notifyIMessageListView(msg);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 处理状态消息发送
     *
     * @param text
     */
    private void handleSendUserStateMessage(CharSequence text) {
        if (text == null) {
            return;
        }
        if (text.toString().trim().length() <= 0) {
            return;
        }

        if (CCPAppManager.getUserId().equals(mRecipients)) {
            return;
        }

        // 组建一个待发送的ECMessage
        ECMessage msg = ECMessage.createECMessage(ECMessage.Type.STATE);
        // 设置消息接收者
        msg.setTo(mRecipients);
        // 创建一个文本消息体，并添加到消息对象中
        ECUserStateMessageBody msgBody = new ECUserStateMessageBody(text.toString());
        msg.setBody(msgBody);
        ECChatManager ecChatManager = ECDevice.getECChatManager();
        if (ecChatManager == null) {
            return;
        }
        ecChatManager.sendMessage(msg, new ECChatManager.OnSendMessageListener() {
            @Override
            public void onSendMessageComplete(ECError error, ECMessage message) {

            }

            @Override
            public void onProgress(String msgId, int totalByte, int progressByte) {

            }
        });


    }

    private void handleSendLocationMessage(LocationInfo locationInfo) {
        if (locationInfo == null) {
            return;
        }
        String address = locationInfo.getAddress();
        if (TextUtils.isEmpty(address)) {
            return;
        }

        // 组建一个待发送的ECMessage
        ECMessage msg = ECMessage.createECMessage(ECMessage.Type.LOCATION);
        // 设置消息接收者
        msg.setTo(mRecipients);
        // 创建一个文本消息体，并添加到消息对象中
        ECLocationMessageBody msgBody = new ECLocationMessageBody(locationInfo.getLat(), locationInfo.getLon());
        msgBody.setTitle(locationInfo.getAddress());
        msg.setBody(msgBody);
        try {
            // 发送消息，该函数见上
            long rowId = -1;
            if (mCustomerService) {
                rowId = CustomerServiceHelper.sendMCMessage(msg);
            } else {
                rowId = IMChattingHelper.sendECMessage(msg);
            }
            // 通知列表刷新
            msg.setId(rowId);
            notifyIMessageListView(msg);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 不允许发送空白消息
     */
    private void canotSendEmptyMessage() {

        ECAlertDialog buildAlert = ECAlertDialog.buildAlert(getActivity(), R.string.chatting_empty_message_cant_be_sent, R.string.dialog_btn_confim, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mChattingFooter.setEditTextNull();
            }
        });
        buildAlert.setTitle(R.string.app_tip);
        buildAlert.setCanceledOnTouchOutside(false);
        buildAlert.show();
    }

    /**
     * 处理发送附件消息
     *
     * @param length
     * @param pathName
     */
    private void handleSendFileAttachMessage(long length, String pathName) {
        if (TextUtils.isEmpty(pathName)) {
            return;
        }
        // 组建一个待发送的附件ECMessage
        ECMessage msg = ECMessage.createECMessage(ECMessage.Type.FILE);
        // 设置接收者
        msg.setTo(mRecipients);
        // 创建附件消息体
        ECFileMessageBody msgBody = new ECFileMessageBody();
        // 设置附件名
        msgBody.setFileName(DemoUtils.getFilename(pathName));
        // 设置附件扩展名
        msgBody.setFileExt(DemoUtils.getExtensionName(pathName));
        // 设置附件本地路径
        msgBody.setLocalUrl(pathName);
        // 设置附件长度
        msgBody.setLength(length);
        msg.setBody(msgBody);

        try {
            // 调用发送API
            // 发送消息，该函数见上
            long rowId = -1;
            if (mCustomerService) {
                rowId = CustomerServiceHelper.sendMCMessage(msg);
            } else {
                rowId = IMChattingHelper.sendECMessage(msg);
            }
            // 通知列表刷新
            msg.setId(rowId);
            notifyIMessageListView(msg);
        } catch (Exception e) {
        }
    }

    /**
     * 处理发送图片消息
     *
     * @param imgInfo
     */
    public void handleSendImageMessage(ImgInfo imgInfo) {
        String fileName = imgInfo.getBigImgPath();
        String fileUrl = FileAccessor.getImagePathName() + "/" + fileName;
        if (new File(fileUrl).exists()) {
            // 组建一个待发送的ECMessage
            ECMessage msg = ECMessage.createECMessage(ECMessage.Type.IMAGE);
            // 设置接收者
            msg.setTo(mRecipients);
            // 设置附件包体（图片也是相当于附件）
            ECImageMessageBody msgBody = new ECImageMessageBody();

            // 设置附件名
            msgBody.setFileName(fileName);
            // 设置附件扩展名
            msgBody.setFileExt(DemoUtils.getExtensionName(fileName));
            // 设置附件本地路径
            msgBody.setLocalUrl(fileUrl);
            msg.setBody(msgBody);
            try {
                long rowId;
                if (mCustomerService) {
                    rowId = CustomerServiceHelper.sendImageMessage(imgInfo, msg);
                } else {
                    rowId = IMChattingHelper.sendImageMessage(imgInfo, msg);
                }
                // 通知列表刷新
                msg.setId(rowId);
                notifyIMessageListView(msg);
            } catch (Exception e) {
            }
        }
    }

    /**
     * 将发送的消息放入消息列表
     *
     * @param message
     */
    public void notifyIMessageListView(ECMessage message) {
        if (!checkUserThread()) {
            return;
        }
        mListView.setSelection(mListView.getCount() - 1);
    }

    @Override
    protected void handleReceiver(Context context, Intent intent) {
        super.handleReceiver(context, intent);
        if (IMessageSqlManager.ACTION_GROUP_DEL.equals(intent.getAction()) && intent.hasExtra("group_id")) {
            String id = intent.getStringExtra("group_id");
            if (id != null && id.equals(mRecipients)) {
                setIsFinish(true);
                finish();
            }
        } else if (IMChattingHelper.INTENT_ACTION_CHAT_USER_STATE.equals(intent.getAction())) {

            String state = intent.getStringExtra(IMChattingHelper.USER_STATE);
            if (!TextUtils.isEmpty(state) && Integer.parseInt(state) == ImUserState.WRITE.ordinal()) {
                setActionBarTitle("Writing...");
            } else if (!TextUtils.isEmpty(state) && Integer.parseInt(state) == ImUserState.RECORDE.ordinal()) {
                setActionBarTitle("Recording...");
            } else {
                setActionBarTitle(mUsername);
            }

        } else if (IMChattingHelper.INTENT_ACTION_CHAT_EDITTEXT_FOUCU.equals(intent.getAction())) {

            boolean hasFoucs = intent.getBooleanExtra("hasFoucs", false);
            if (hasFoucs) {
                handleSendUserStateMessage("1");
            } else {
                handleSendUserStateMessage("0");
            }


        }
    }

    /**
     * <error code="SdkErrorCode.NON_GROUPMEMBER">文件上传发送者不在群组内</error>
     * <error code="SdkErrorCode.SPEAK_LIMIT_FILE">文件上传接受者被禁言</error>
     * 消息发送报告
     */
    @Override
    public void onMessageReport(ECError error, ECMessage message) {
        if (mChattingAdapter != null) {
            mChattingAdapter.notifyChange();
        }
        if (error == null) {
            return;
        }
        if ((SdkErrorCode.SPEAK_LIMIT_FILE == error.errorCode || SdkErrorCode.SPEAK_LIMIT_TEXT == error.errorCode)) {
            // 成员被禁言
            showAlertTips(R.string.sendmsg_error_15032);
            return;
        }
        if ((SdkErrorCode.NON_GROUPMEMBER == error.errorCode)) {
            // 文件上传发送者不在群组内
            showAlertTips(R.string.sendmsg_error_16072);
            return;
        }

        if (SdkErrorCode.SDK_TEXT_LENGTH_LIMIT == error.errorCode) {
            // 文本长度超过限制
            showAlertTips(R.string.sendmsg_error_170001);
        }
    }

    private void showAlertTips(int message) {
        ECAlertDialog buildAlert = ECAlertDialog.buildAlert(getActivity(), message, R.string.dialog_btn_confim, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        buildAlert.setTitle(R.string.app_tip);
        buildAlert.setCanceledOnTouchOutside(false);
        buildAlert.show();
    }

    public boolean checkUserThread() {
        ChattingListAdapter2 forceAdapter = mChattingAdapter;
        if (forceAdapter == null) {
            return false;
        }
        if (mThread <= 0 || mThread != forceAdapter.getThread()) {
            mThread = forceAdapter.setUsername(mRecipients);
        }
        forceAdapter.notifyChange();
        return true;
    }

    /**
     * 收到新的Push消息
     */
    @Override
    public void onPushMessage(String sid, List<ECMessage> msgs) {

        if (!mRecipients.equals(sid)) {
            return;
        }

        if (!checkUserThread()) {
            return;
        }
        showMsgLayoutMask();
        // 当前是否正在查看消息
        if (!isViewMode) mListView.setSelection(mListView.getCount() - 1);

        setChattingSessionRead();
    }

    /**
     * 更新所有的未读消息
     */
    private void setChattingSessionRead() {
        ConversationSqlManager.setChattingSessionRead(mThread);
    }

    /**
     * 给予客户端震动提示
     */
    protected void readyOperation() {
        computationTime = -1L;
        mRecordTipsToast = null;
        playTone(ToneGenerator.TONE_PROP_BEEP, TONE_LENGTH_MS);
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                stopTone();
            }
        }, TONE_LENGTH_MS);
        vibrate(50L);
    }

    private Object mToneGeneratorLock = new Object();

    // 初始化
    private void initToneGenerator() {
        AudioManager mAudioManager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
        if (mToneGenerator == null) {
            try {
                int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
                int streamMaxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
                int volume = (int) (TONE_RELATIVE_VOLUME * (streamVolume / streamMaxVolume));
                mToneGenerator = new ToneGenerator(AudioManager.STREAM_MUSIC, volume);

            } catch (RuntimeException e) {
                LogUtil.d("Exception caught while creating local tone generator: "
                        + e);
                mToneGenerator = null;
            }
        }
    }

    /**
     * 停止播放声音
     */
    public void stopTone() {
        if (mToneGenerator != null)
            mToneGenerator.stopTone();
    }

    /**
     * 播放提示音
     *
     * @param tone
     * @param durationMs
     */
    public void playTone(int tone, int durationMs) {
        synchronized (mToneGeneratorLock) {
            initToneGenerator();
            if (mToneGenerator == null) {
                LogUtil.d("playTone: mToneGenerator == null, tone: " + tone);
                return;
            }

            // Start the new tone (will stop any playing tone)
            mToneGenerator.startTone(tone, durationMs);
        }
    }

    /**
     * 手机震动
     *
     * @param milliseconds
     */
    public synchronized void vibrate(long milliseconds) {
        Vibrator mVibrator = (Vibrator) getActivity().getSystemService(
                Context.VIBRATOR_SERVICE);
        if (mVibrator == null) {
            return;
        }
        mVibrator.vibrate(milliseconds);
    }

    public void showTakeStyle(final Context ctx) {
        handleTackPicture();
    }

    private void handleTackPicture() {
        if (!FileAccessor.isExistExternalStore()) {
            return;
        }
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File file = FileAccessor.getTackPicFilePath();
        if (file != null) {
            Uri uri = Uri.fromFile(file);
            if (uri != null) {
                intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
            }
            mFilePath = file.getAbsolutePath();
        }
        startActivityForResult(intent, REQUEST_CODE_TAKE_PICTURE);
    }

    private void scrollListViewToLast() {
        if (mListView != null) {
            mListView.postDelayed(new Runnable() {

                @Override
                public void run() {
                    int lastVisiblePosition = mListView.getLastVisiblePosition();
                    int count = mListView.getCount() - 1;
                    LogUtil.v(LogUtil.TAG + "ChattingFooterEventImpl", "last visible/adapter=" + lastVisiblePosition + "/" + count);

                    if (mListView.getCount() <= 1) {
                        SmoothScrollToPosition.setSelection(mListView, count, true);
                        return;
                    }
                    SmoothScrollToPosition.setSelectionFromTop(mListView, count - 1, 0, true);
                }
            }, 10L);
        }
    }

    private void handleSelectImageIntent() {
        Intent intent = new Intent(this.getActivity(), PhotoPickerActivity.class);
        intent.putExtra(PhotoPickerActivity.EXTRA_SHOW_CAMERA, true);
        intent.putExtra(PhotoPickerActivity.EXTRA_SELECT_MODE, PhotoPickerActivity.MODE_SINGLE);
        intent.putExtra(PhotoPickerActivity.EXTRA_MAX_MUN, 9);
        startActivityForResult(intent, REQUEST_CODE_LOAD_IMAGE);
    }

    /**
     * 消息重发
     *
     * @param msg
     * @param position
     */
    public void doResendMsgRetryTips(final ECMessage msg, final int position) {
        ECAlertDialog buildAlert = ECAlertDialog.buildAlert(getActivity(), R.string.chatting_resend_content, null, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                resendMsg(msg, position);
            }
        });
        buildAlert.setTitle(R.string.chatting_resend_title);
        buildAlert.show();
    }

    private void keepScreenOnState(boolean screenOn) {
        if (mListView != null) {
            mListView.setKeepScreenOn(screenOn);
        }
    }

    /**
     * @param msg
     * @param position
     */
    protected void resendMsg(ECMessage msg, int position) {
        if (msg == null || position < 0 || mChattingAdapter.getItem(position) == null) {
            LogUtil.d(TAG, "ignore resend msg , msg " + msg + " , position " + position);
            return;
        }
        ECMessage message = mChattingAdapter.getItem(position);
        message.setTo(mRecipients);
        long rowid = IMChattingHelper.reSendECMessage(message);
        if (rowid != -1) {
            mChattingAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void toRequest(int eventTag) {

        if (eventTag == ApiConstants.EventTags.GIFT_SYSTEM_LIST_REFRESH) {
            //系统礼物列表刷新

            mSystemCurrentPage = 1;

            RequestParams params = new RequestParams();
            params.put("page", mSystemCurrentPage + "");
            params.put("pageSize", 20);

            iCommonRequestPresenter.request(eventTag, getContext(), ApiConstants.Urls.GIFT_SYSTEM_LIST, params);

        } else if (eventTag == ApiConstants.EventTags.GIFT_SYSTEM_LIST_LOAD_MORE) {
            //系统礼物列表加载更多

            mSystemCurrentPage = mSystemCurrentPage + 1;

            RequestParams params = new RequestParams();
            params.put("page", mSystemCurrentPage + "");
            params.put("pageSize", 20);

            iCommonRequestPresenter.request(eventTag, getContext(), ApiConstants.Urls.GIFT_SYSTEM_LIST, params);

        } else if (eventTag == ApiConstants.EventTags.GIFT_RECEIVER_LIST_REFRESH) {
            //收到的礼物列表刷新

            mReceiverCurrentPage = 1;

            RequestParams params = new RequestParams();

            params.put("page", mReceiverCurrentPage + "");
            params.put("pageSize", 50);
            params.put("windows", "2");

            iCommonRequestPresenter.request(eventTag, getContext(), ApiConstants.Urls.GIFT_RECEIVER_LIST, params);


        } else if (eventTag == ApiConstants.EventTags.GIFT_RECEIVER_LIST_LOAD_MORE) {
            //收到的礼物列表加载更多

            mReceiverCurrentPage = mReceiverCurrentPage + 1;

            RequestParams params = new RequestParams();
            params.put("page", mReceiverCurrentPage + "");
            params.put("pageSize", 50);
            params.put("windows", "2");

            iCommonRequestPresenter.request(eventTag, getContext(), ApiConstants.Urls.GIFT_RECEIVER_LIST, params);

        } else if (eventTag == ApiConstants.EventTags.ACCOUNT_INFO) {
            //会员个人信息

            RequestParams params = new RequestParams();
            params.put("id", mRecipients);

            iCommonRequestPresenter.request(eventTag, getContext(), ApiConstants.Urls.ACCOUNT_INFO, params);
        } else if (eventTag == ApiConstants.EventTags.ACCOUNT_SEND_MESSAGE_BEFORE) {
            //账户发送消息

            RequestParams params = new RequestParams();

            iCommonRequestPresenter.request(eventTag, getContext(), ApiConstants.Urls.ACCOUNT_SEND_MESSAGE_BEFORE, params);
        } else if (eventTag == ApiConstants.EventTags.ACCOUNT_SEND_MESSAGE_AFTER) {
            //账户发送消息

            RequestParams params = new RequestParams();

            iCommonRequestPresenter.request(eventTag, getContext(), ApiConstants.Urls.ACCOUNT_SEND_MESSAGE_AFTER, params);
        }
    }

    @Override
    public void getRequestData(int eventTag, String result) {

        Log.e("result", result);

        JsonHelper<Gift> jsonHelper = new JsonHelper<Gift>(Gift.class);
        List<Gift> list = jsonHelper.getDatas(result, "list");

        if (eventTag == ApiConstants.EventTags.GIFT_SYSTEM_LIST_REFRESH) {
            //系统礼物列表刷新
            mResultList.clear();
            mResultList.addAll(list);

            refreshListView();

        } else if (eventTag == ApiConstants.EventTags.GIFT_SYSTEM_LIST_LOAD_MORE) {
            //系统礼物列表加载更多

            mResultList.addAll(list);
            adapter.notifyDataSetChanged();

        } else if (eventTag == ApiConstants.EventTags.GIFT_RECEIVER_LIST_REFRESH) {
            //收到的礼物列表刷新

            mResultList.clear();
            mResultList.addAll(list);

            refreshListView();

        } else if (eventTag == ApiConstants.EventTags.GIFT_RECEIVER_LIST_LOAD_MORE) {
            //收到的礼物列表加载更多
            mResultList.addAll(list);
            adapter.notifyDataSetChanged();
        } else if (eventTag == ApiConstants.EventTags.GIFT_HANDLE) {
            //礼物购买，兑换

            for (int i = 0; i < mResultList.size(); i++) {
                if (mResultList.get(i).isSelect()) {
                    String giftImageUrl = mResultList.get(i).getUrl();
                    String giftId = mResultList.get(i).getId() + "";
                    String giftName = mResultList.get(i).getName() + "";

                    String giftText = GiftContacts.getGiftContact(giftImageUrl, giftId, giftName);

                    handlesendGiftMessage(giftText);

                }
            }

            mGiftsLayout.setVisibility(View.GONE);

        } else if (eventTag == ApiConstants.EventTags.ACCOUNT_INFO) {
            //会员个人信息

            JsonHelper<UserInfo> dataParser = new JsonHelper<UserInfo>(
                    UserInfo.class);
            UserInfo userInfo = dataParser.getData(
                    result, "result");

            String accountName = userInfo.getAccountName();

            RequestParams params = new RequestParams();
            params.put("receiveAccount", accountName);
            params.put("operate", "0");

            for (int i = 0; i < mResultList.size(); i++) {

                Gift gift = mResultList.get(i);
                if (gift.isSelect()) {
                    params.put("giftId", gift.getId() + "");

                    if (CommonUtils.isEmpty(gift.getAccountGiftId())) {
                        params.put("isSystem", "0");
                        params.put("accountGiftId", "");
                    } else {
                        params.put("isSystem", "1");
                        params.put("accountGiftId", "" + gift.getAccountGiftId());
                    }

                }
            }

            iCommonRequestPresenter.request(ApiConstants.EventTags.GIFT_HANDLE, getContext(), ApiConstants.Urls.GIFT_HANDLE, params);
        } else if (eventTag == ApiConstants.EventTags.ACCOUNT_SEND_MESSAGE_BEFORE) {
            mChattingFooterImpl.OnSendTextMessageRequest(mInputText);

            if (!CommonUtils.isEmpty(mInputText.toString())) {
                toRequest(ApiConstants.EventTags.ACCOUNT_SEND_MESSAGE_AFTER);
            }
        }
    }

    @Override
    public void onRequestSuccessException(int eventTag, String msg) {
        Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();

        if (eventTag == ApiConstants.EventTags.GIFT_SYSTEM_LIST_LOAD_MORE) {
            //系统礼物列表加载更多
            if (mSystemCurrentPage > 1)
                mSystemCurrentPage = mSystemCurrentPage - 1;

        } else if (eventTag == ApiConstants.EventTags.GIFT_RECEIVER_LIST_LOAD_MORE) {
            //收到的礼物列表加载更多
            if (mReceiverCurrentPage > 1)
                mReceiverCurrentPage = mReceiverCurrentPage - 1;
        }
    }

    @Override
    public void onRequestFailureException(int eventTag, String msg) {

        if (!CommonUtils.isEmpty(msg)) {
            Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
        }


        if (eventTag == ApiConstants.EventTags.GIFT_SYSTEM_LIST_LOAD_MORE) {
            //系统礼物列表加载更多
            if (mSystemCurrentPage > 1)
                mSystemCurrentPage = mSystemCurrentPage - 1;

        } else if (eventTag == ApiConstants.EventTags.GIFT_RECEIVER_LIST_LOAD_MORE) {
            //收到的礼物列表加载更多
            if (mReceiverCurrentPage > 1)
                mReceiverCurrentPage = mReceiverCurrentPage - 1;
        }
    }

    @Override
    public void isRequesting(int eventTag, boolean status) {
        if (eventTag == ApiConstants.EventTags.GIFT_SYSTEM_LIST_REFRESH) {
            //系统礼物列表刷新

            if (status) {
                mPostingdialog = new ECProgressDialog(getContext(), R.string.loading_gifts);
                mPostingdialog.show();
            } else {
                mPostingdialog.dismiss();
            }

        } else if (eventTag == ApiConstants.EventTags.GIFT_RECEIVER_LIST_LOAD_MORE) {
            //系统礼物列表加载更多

        } else if (eventTag == ApiConstants.EventTags.GIFT_RECEIVER_LIST_REFRESH) {
            //收到的礼物列表刷新

            if (status) {
                mPostingdialog = new ECProgressDialog(getContext(), R.string.loading_gifts);
                mPostingdialog.show();
            } else {
                mPostingdialog.dismiss();
            }

        } else if (eventTag == ApiConstants.EventTags.GIFT_RECEIVER_LIST_REFRESH) {
            //收到的礼物列表加载更多

        } else if (eventTag == ApiConstants.EventTags.GIFT_HANDLE) {
            //礼物购买，兑换
            if (status) {
                mPostingdialog = new ECProgressDialog(getContext(), R.string.loading);
                mPostingdialog.show();
            } else {
                mPostingdialog.dismiss();
            }
        } else if (eventTag == ApiConstants.EventTags.ACCOUNT_INFO) {
            //会员个人信息
            if (status) {
                mPostingdialog = new ECProgressDialog(getContext(), R.string.loading);
                mPostingdialog.show();
            } else {
                mPostingdialog.dismiss();
            }
        } else if (eventTag == ApiConstants.EventTags.ACCOUNT_SEND_MESSAGE_BEFORE) {
            //账号发送消息
            if (status) {
                mPostingdialog = new ECProgressDialog(getContext(), R.string.loading);
                mPostingdialog.show();
            } else {
                mPostingdialog.dismiss();
            }
        }
    }

    /**
     * 聊天插件功能实现
     */
    private class OnOnChattingPanelImpl implements CCPChattingFooter2.OnChattingPanelClickListener {

        @Override
        public void OnTakingPictureRequest() {

            showTakeStyle(getActivity());
            hideBottomPanel();
        }

        @Override
        public void OnSelectImageReuqest() {
            handleSelectImageIntent();
            hideBottomPanel();
        }

        @Override
        public void OnSelectFileRequest() {
            startActivityForResult(new Intent(getActivity(),
                    FileExplorerActivity.class), 0x2a);
            hideBottomPanel();
        }

        private void hideBottomPanel() {
            mChattingFooter.hideBottomPanel();
        }

        @Override
        public void OnSelectVoiceRequest() {

            if (!isPeerChat()) {
                handleVoiceCall();
                hideBottomPanel();
            } else {
                handleGroupVoiceCall();
            }
        }

        @Override
        public void OnSelectVideoRequest() {
        }

        @Override
        public void OnSelectFireMsg() {
            showTakeFireStyle(getActivity());
            hideBottomPanel();
        }

        @Override
        public void OnSelectLocationRequest() {//位置

            Intent intent = new Intent(getActivity(), LocationActivity.class);
            startActivityForResult(intent, REQUEST_CODE_TAKE_LOCATION);
            hideBottomPanel();
        }

        @Override
        public void OnSelectRedPacketRequest() {

            //传递到sdk里的数据
            com.alibaba.fastjson.JSONObject jsonObject = new com.alibaba.fastjson.JSONObject();
            //传递参数到红包sdk：发送者头像url，昵称（缺失则传id）
            String fromAvatarUrl = "none";
            String fromNickName = clientUser.getUserName();
            fromNickName = TextUtils.isEmpty(fromNickName) ? clientUser.getUserId() : fromNickName;
            jsonObject.put(RedPacketConstant.KEY_FROM_AVATAR_URL, fromAvatarUrl);
            jsonObject.put(RedPacketConstant.KEY_FROM_NICK_NAME, fromNickName);
            jsonObject.put(RedPacketConstant.KEY_CURRENT_ID, CCPAppManager.getUserId());
            if (!isPeerChat()) {
                //如果是单聊传递对方id
                jsonObject.put(RedPacketConstant.KEY_USER_ID, mRecipients);
                jsonObject.put(RedPacketConstant.KEY_CHAT_TYPE, 1);
            } else {
                //如果是群聊传递群id和群人数
                ECGroup ecGroup = GroupSqlManager.getECGroup(mRecipients);
                jsonObject.put(RedPacketConstant.KEY_GROUP_ID, ecGroup.getGroupId());
                jsonObject.put(RedPacketConstant.KEY_GROUP_MEMBERS_COUNT, ecGroup.getCount());
                jsonObject.put(RedPacketConstant.KEY_CHAT_TYPE, 2);
            }
            RedPacketUtil.startRedPacketActivityForResult(ChattingFragment.this, jsonObject, REQUEST_CODE_REDPACKET);
            hideBottomPanel();
        }

        @Override
        public void OnClickTopicRequest() {
        }

        @Override
        public void OnClickGiftRequest() {
            //点击礼物
            mGiftsLayout.setVisibility(View.VISIBLE);

            toRequest(ApiConstants.EventTags.GIFT_SYSTEM_LIST_REFRESH);
        }

        @Override
        public void OnClickInputRequest() {
        }
    }

    ClientUser clientUser = CCPAppManager.getClientUser();
    public static final int REQUEST_CODE_REDPACKET = 99;
    public static boolean isFireMsg = false;

    public void showTakeFireStyle(final Context ctx) {//阅后即焚
        ECListDialog dialog = new ECListDialog(ctx, R.array.take_chat_fire_msg);
        ;
        dialog.setOnDialogItemClickListener(new ECListDialog.OnDialogItemClickListener() {
            @Override
            public void onDialogItemClick(Dialog d, int position) {
                LogUtil.d("onDialogItemClick", "position " + position);
                isFireMsg = true;
                if (position == 0) {
                    handleTackPicture();

                } else if (position == 1) {
                    handleSelectImageIntent();
                }

            }
        });
        dialog.setTitle(R.string.take_title);
        dialog.show();
    }

    BQMMEditView mInputEt;
    CharSequence mInputText;

    /**
     * 聊天功能面板（发送、录音、切换输入选项）
     */
    private class OnChattingFooterImpl implements
            CCPChattingFooter2.OnChattingFooterLinstener {

        ChattingActivity mActivity;
        protected String mVoicePathName;
        /**
         * 保存当前的录音状态
         */
        public int mRecordState = RECORD_IDLE;
        /**
         * 语音录制空闲
         */
        public static final int RECORD_IDLE = 0;
        /**
         * 语音录制中
         */
        public static final int RECORD_ING = 1;

        /**
         * 待发的ECMessage消息
         */
        private ECMessage mPreMessage;
        MediaPlayTools instance;

        /**
         * 同步锁
         */
        Object mLock = new Object();

        public OnChattingFooterImpl(ChattingActivity ctx) {
            mActivity = ctx;
            instance = MediaPlayTools.getInstance();
        }

        @Override
        public void OnVoiceRcdInitReuqest() {
            mVoicePathName = String.valueOf(System
                    .currentTimeMillis()) + ".m4a";
            if (FileAccessor.getVoicePathName() == null) {
                ToastUtil.showMessage("Path to file could not be created");
                mVoicePathName = null;
                return;
            }
            keepScreenOnState(true);
            if (getRecordState() != RECORD_ING) {
                setRecordState(RECORD_ING);

                // 手指按下按钮，按钮给予振动或者声音反馈
                readyOperation();
                // 显示录音提示框
                mChattingFooter.showVoiceRecordWindow(findViewById(R.id.chatting_bg_ll).getHeight() - mChattingFooter.getHeight());

                final ECChatManager chatManager = SDKCoreHelper.getECChatManager();
                if (chatManager == null) {
                    return;
                }
                mVoiceHandler.post(new Runnable() {

                    @SuppressWarnings("deprecation")
                    @Override
                    public void run() {
                        try {
                            // 仅录制语音消息，录制完成后需要调用发送接口发送消息
                            handleSendUserStateMessage("2");

                            audioFile = new File(FileAccessor.getVoicePathName(), mVoicePathName);
                            recorderUtils = new AudioRecoderUtils(audioFile);

                            recorderUtils.setOnAudioStatusUpdateListener(new AudioRecoderUtils.OnAudioStatusUpdateListener() {
                                public void onUpdate(double db)
                                {
                                    if (mChattingFooter != null && getRecordState() == RECORD_ING) {
                                        mChattingFooter.showVoiceRecording();
                                        mChattingFooter.displayAmplitude(db);
                                    }
                                }
                            });

                            recorderUtils.startRecord();
                            mChattingFooter.showVoiceRecording();

                        } catch (Exception e) {
                            LogUtil.e(TAG, "请检查录音权限是否被禁止");
                            e.printStackTrace();
                        }
                    }


                });
            }
        }

        @Override
        public void OnVoiceRcdStartRequest() {
            // SDK完成初始化底层音频设备、开始采集音频数据
            mHandler.removeMessages(WHAT_ON_COMPUTATION_TIME);
            mHandler.sendEmptyMessageDelayed(WHAT_ON_COMPUTATION_TIME, TONE_LENGTH_MS);
        }

        @Override
        public void OnVoiceRcdCancelRequest() {
            handleSendUserStateMessage("0");
            handleMotionEventActionUp(true, false);
            recorderUtils.stopRecord();
            audioFile.delete();
        }

        @Override
        public void OnVoiceRcdStopRequest(boolean isSend) {
            recorderUtils.stopRecord();

            ECMessage message = ECMessage.createECMessage(ECMessage.Type.VOICE);
            message.setTo(mRecipients);

            ECVoiceMessageBody messageBody = new ECVoiceMessageBody(audioFile, 0);
            message.setBody(messageBody);
            mPreMessage = message;

            handleSendUserStateMessage("0");
            handleMotionEventActionUp(false, isSend);

        }

        @Override
        public void OnSendTextMessageRequest(CharSequence text) {
            if (text != null && text.toString().trim().startsWith("starttest://")) {

                handleTest(text.toString().substring("starttest://".length()));
                return;
            } else if (text != null && text.toString().trim().startsWith("endtest://")) {
                debugeTest = false;
                return;
            }


            handleSendTextMessage(text);
        }

        @Override
        public void OnUpdateTextOutBoxRequest(CharSequence text) {
        }

        @Override
        public void OnSendCustomEmojiRequest(int emojiid, String emojiName) {
        }

        @Override
        public void OnEmojiDelRequest() {
        }

        @Override
        public void OnSendInputMessage(BQMMEditView inputEt, CharSequence text) {
            //点击发送按钮
            mInputEt = inputEt;
            mInputText = text;

            LogUtil.e("result", text + "");

            if (isPeerChat()) {
                // 群聊'
                mChattingFooterImpl.OnSendTextMessageRequest(mInputText);
            } else {
                // 单聊
                toRequest(ApiConstants.EventTags.ACCOUNT_SEND_MESSAGE_BEFORE);
            }
        }

        @Override
        public void OnInEditMode() {
            scrollListViewToLast();
            //mChattingFooter.keyboardHack.setVisibility(View.VISIBLE);
        }

        @Override
        public void onPause() {
            stopPlayVoice();
        }

        @Override
        public void onResume() {

        }

        @Override
        public void release() {
            mActivity = null;
            mPreMessage = null;
        }

        /**
         * 处理Button 按钮按下抬起事件
         *
         * @param doCancle 是否取消或者停止录制
         */
        private void handleMotionEventActionUp(final boolean doCancle, boolean isSend) {
            keepScreenOnState(false);
            if (getRecordState() == RECORD_ING) {
                doVoiceRecordAction(doCancle, isSend);
            }
        }

        /**
         * 处理语音录制结束事件
         *
         * @param doCancle 是否取消或者停止录制
         */
        private void doVoiceRecordAction(boolean doCancle, final boolean isSend) {
            final boolean cancleVoice = doCancle;

            if (mChatManager != null) {
                mVoiceHandler.post(new Runnable() {

                    @Override
                    public void run() {
                        // 停止或者取消普通模式语音
                        LogUtil.d(LogUtil.getLogUtilsTag(getClass()), "handleMotionEventActionUp stop normal record");
                        mChatManager.stopVoiceRecording(new ECChatManager.OnStopVoiceRecordingListener() {
                            @Override
                            public void onRecordingComplete() {
                                LogUtil.d(TAG, "onRecordingComplete");
                                doProcesOperationRecordOver(cancleVoice, isSend);
                            }
                        });
                    }
                });
            }
        }

        /**
         * 处理录音结束消息是否发送逻辑
         *
         * @param cancel 是否取消发送
         */
        protected void doProcesOperationRecordOver(boolean cancel, boolean isSend) {
            if (getRecordState() == RECORD_ING) {
                // 当前是否有正在录音的操作
                // 定义标志位判断当前所录制的语音文件是否符合发送条件
                // 只有当录制的语音文件的长度超过1s才进行发送语音
                boolean isVoiceToShort = false;
                File amrPathFile = new File(FileAccessor.getVoicePathName(), mVoicePathName);
                if (amrPathFile.exists()) {
                    mVoiceRecodeTime = DemoUtils.calculateVoiceTime(amrPathFile.getAbsolutePath());
                    if (!isRecordAndSend) {
                        if (mVoiceRecodeTime * 1000 < MIN_TIME) {
                            isVoiceToShort = true;
                        }
                    }
                } else {
                    isVoiceToShort = true;
                }

                // 设置录音空闲状态
                setRecordState(RECORD_IDLE);
                if (mChattingFooter != null) {
                    if (isVoiceToShort && !cancel) {
                        // 提示语音文件长度太短
                        mChattingFooter.tooShortPopuWindow();
                        return;
                    }

                    // 关闭语音录制对话框
                    mChattingFooter.dismissPopuWindow();
                }

                if (!cancel && mPreMessage != null && isSend) {
                    if (!isRecordAndSend) {
                        // 如果当前的录音模式为非Chunk模式
                        try {
                            ECVoiceMessageBody body = (ECVoiceMessageBody) mPreMessage.getBody();
                            body.setDuration(mVoiceRecodeTime);
                            long rowId;
                            if (mCustomerService) {
                                rowId = CustomerServiceHelper.sendMCMessage(mPreMessage);
                            } else {
                                rowId = IMChattingHelper.sendECMessage(mPreMessage);
                            }
                            mPreMessage.setId(rowId);
                            notifyIMessageListView(mPreMessage);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    return;
                }

                // 删除语音文件
                if (amrPathFile.exists()) {
                    amrPathFile.deleteOnExit();
                }
                
                // 重置语音时间长度统计
                mVoiceRecodeTime = 0;
            }
        }

        public int getRecordState() {
            synchronized (mLock) {
                return mRecordState;
            }
        }

        public void setRecordState(int state) {
            synchronized (mLock) {
                this.mRecordState = state;
            }
        }

        /*@Override
        public void onVoiceChangeRequest(int position) {
        }


        public void sendChangeVoiceMsg(boolean isSendYuanSheng) {
        }*/

        @Override
        public void stopVoicePlay() {
            // TODO Auto-generated method stub
            if (instance != null && instance.isPlaying()) {
                instance.stop();
            }
        }
    }

    private void stopPlayVoice() {
        if (mChattingAdapter != null) {
            // 停止播放语音
            mChattingAdapter.onPause();
            mChattingAdapter.notifyDataSetChanged();
        }
    }

    public void handleVoiceCall() {
        ECContacts contact = ContactSqlManager.getContact(mRecipients);
        if (contact == null) {
            return;
        }
        CCPAppManager.callVoIPAction(getActivity(), CallType.VOICE,
                contact.getNickname(), contact.getContactid(), false);

    }

    public class ChattingAsyncTask extends ECAsyncTask {

        /**
         * @param context
         */
        public ChattingAsyncTask(Context context) {
            super(context);
        }

        @Override
        protected Object doInBackground(Object... params) {
            ImgInfo createImgInfo;
            if (((String) params[0]).endsWith(".gif")) {
                createImgInfo = ImgInfoSqlManager.getInstance().createGIFImgInfo((String) params[0]);
            } else {
                createImgInfo = ImgInfoSqlManager.getInstance().createImgInfo((String) params[0]);
            }
            return createImgInfo;
        }

        @Override
        protected void onPostExecute(Object result) {
            if (result instanceof ImgInfo) {
                ImgInfo imgInfo = (ImgInfo) result;
                handleSendImageMessage(imgInfo);
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_left:
                setIsFinish(true);
                hideSoftKeyboard();
                finish();
                break;
            case R.id.btn_right:
                if (!isPeerChat()) {
                    Intent intent = new Intent(getActivity(), OthersInfoActivity.class);
                    intent.putExtra(OthersInfoActivity.ACCOUNT, mRecipients);
                    startActivity(intent);

                    return;
                }
                // 群组聊天室
                Intent intent = new Intent(getActivity(), GroupInfoActivity.class);
                intent.putExtra(GroupInfoActivity.GROUP_ID, mRecipients);
                startActivityForResult(intent, REQUEST_VIEW_CARD);
                break;
            case R.id.btn_middle:
                if (mListView != null) {
                    getTopBarView().post(new Runnable() {
                        @Override
                        public void run() {
                            mListView.setSelection(mChattingAdapter.getCount());
                        }
                    });
                }
                break;
            default:
                break;
        }
    }

    private OnRefreshAdapterDataListener mOnRefreshAdapterDataListener = new OnRefreshAdapterDataListener() {

        @Override
        public void refreshData() {//刷新数据
            if (getActivity() == null || getActivity().isFinishing()) {
                return;
            }
            int size = mChattingAdapter.increaseCount();
            mChattingAdapter.checkTimeShower();
            mChattingAdapter.notifyChange();
            int count = mChattingAdapter.getCount() - size;
            LogUtil.d(TAG, "onRefreshing history msg count " + count);
            mListView.setSelectionFromTop(size + 1, mListViewHeadView.getHeight() + mECPullDownView.getTopViewHeight());
        }

    };

    private OnListViewBottomListener mOnListViewBottomListener = new OnListViewBottomListener() {

        @Override
        public boolean getIsListViewToBottom() {
            View lastChildAt = mListView.getChildAt(mListView.getChildCount() - 1);
            if (lastChildAt == null) {
                return false;
            }
            if ((lastChildAt.getBottom() <= mListView.getHeight())
                    && mListView.getLastVisiblePosition() == mListView.getAdapter().getCount() - 1) {
                return true;
            }
            return false;
        }
    };

    private OnListViewTopListener mOnListViewTopListener = new OnListViewTopListener() {

        @Override
        public boolean getIsListViewToTop() {
            View topChildAt = mListView.getChildAt(mListView.getFirstVisiblePosition());
            return ((topChildAt != null) && (topChildAt.getTop() == 0));
        }
    };

    String fileUrl = "";
    int type = 0;//0图片，1音频
    String collectName = "";
    private AdapterView.OnItemLongClickListener mOnItemLongClickListener
            = new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            final int itemPosition = position;
            if (mChattingAdapter != null) {
                int headerViewsCount = mListView.getHeaderViewsCount();
                if (itemPosition < headerViewsCount) {
                    return false;
                }
                int _position = itemPosition - headerViewsCount;

                if (mChattingAdapter == null || mChattingAdapter.getItem(_position) == null) {
                    return false;
                }

                ECMessage item = mChattingAdapter.getItem(_position);
                String title = " ";

                int arrRes = R.array.chat_menu_left;//默认是左边的菜单选项
                item.setSessionId(mRecipients);
                if (item.getDirection() == ECMessage.Direction.SEND) {
                    if (isPeerChat()) {//判断是否是群组
                        if (isVail(item)) {
                            arrRes = R.array.chat_menu_group;
                        } else {
                            arrRes = R.array.chat_menu_group_back;
                        }
                    } else {
                        if (isVail(item)) {
                            arrRes = R.array.chat_menu;
                        } else {
                            arrRes = R.array.chat_menu_no_back;
                        }
                    }
                }
                ECListDialog dialog;
                if (item.getType() == ECMessage.Type.TXT) {
                    ECTextMessageBody textBody = (ECTextMessageBody) item.getBody();
                    String content = textBody.getMessage();
                    // 文本有复制功能
                    if (isVail(item)) {
                    }
                    if (content.startsWith("[gift]") || content.startsWith("[topic]")) {
                        dialog = null;
                    } else {
                        dialog = new ECListDialog(getActivity(), arrRes);
                    }

                } else if (item.getType() == ECMessage.Type.IMAGE) {

                    ECContacts contact = ContactSqlManager.getContact(item.getForm());
                    String displayName = contact.getNickname();
                    if (contact != null) {
                        String info = displayName;
                        Log.e("info", info);
                        if (info.contains("(")) {
                            int index = info.indexOf("(");
                            String message = info.substring(index + 1, info.length() - 1);
                            UserInfo infos = null;
                            try {
                                infos = parseResult(message);
                                collectName = infos.getNickName();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    } else {
                        collectName = item.getForm();
                    }

                    arrRes = R.array.chat_menu_img_voice;
                    ImgInfo imgInfo = ImgInfoSqlManager.getInstance().getImgInfo(
                            item.getMsgId());
                    fileUrl = FileAccessor.getImagePathName()
                            + "/" + imgInfo.getBigImgPath();
                    Log.e("img", fileUrl);
                    type = 0;
                    dialog = new ECListDialog(getActivity(), arrRes);
                } else if (item.getType() == ECMessage.Type.VOICE) {

                    ECContacts contact = ContactSqlManager.getContact(item.getForm());
                    String displayName = contact.getNickname();
                    if (contact != null) {
                        String info = displayName;
                        Log.e("info", info);
                        if (info.contains("(")) {
                            int index = info.indexOf("(");
                            String message = info.substring(index + 1, info.length() - 1);
                            UserInfo infos = null;
                            try {
                                infos = parseResult(message);
                                collectName = infos.getNickName();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    } else {
                        collectName = item.getForm();
                    }

                    arrRes = R.array.chat_menu_img_voice;
                    ECVoiceMessageBody voiceBody = (ECVoiceMessageBody) item.getBody();
                    fileUrl = voiceBody.getLocalUrl();
                    Log.e("fileLoalPath", fileUrl);
                    type = 1;
                    dialog = new ECListDialog(getActivity(), arrRes);
                } else {
                    dialog = null;
                }
                final int arrFial = arrRes;
                if (dialog != null && arrFial != 0) {

                    dialog.setOnDialogItemClickListener(new ECListDialog.OnDialogItemClickListener() {
                        @Override
                        public void onDialogItemClick(Dialog d, int position) {
                            handleContentMenuClick(itemPosition, position, arrFial);
                        }
                    });
                    dialog.setTitle(title);
                    dialog.show();
                    return true;
                }
            }
            return false;
        }
    };

    private UserInfo parseResult(String result) {
        JsonHelper<UserInfo> dataParser = new JsonHelper<UserInfo>(
                UserInfo.class);
        return dataParser.getData(
                result, "result");
    }

    private boolean isVail(ECMessage msg) {

        if (msg == null) {
            return false;
        }

        long time = System.currentTimeMillis();

        return time - msg.getMsgTime() <= 1000 * 120;
    }

    private Boolean handleContentMenuClick(int convresion, int position, int arr) {
        if (mChattingAdapter != null) {
            int headerViewsCount = mListView.getHeaderViewsCount();
            if (convresion < headerViewsCount) {
                return false;
            }
            int _position = convresion - headerViewsCount;

            if (mChattingAdapter == null || mChattingAdapter.getItem(_position) == null) {
                return false;
            }
            final ECMessage msg = mChattingAdapter.getItem(_position);
            LogUtil.d(TAG, "ECMessage  get msgId is =" + msg.getMsgId());
            msg.setSessionId(mRecipients);

            String[] resarr = getResources().getStringArray(arr);
            String i = resarr[position];

            if ("Delete".endsWith(i)) {
                doDelMsgTips(msg, _position);

            } else if ("撤销".endsWith(i)) {
                ECChatManager chatManager = SDKCoreHelper.getECChatManager();
                if (chatManager == null || msg == null) {
                    return null;
                }

                if (msg.getDirection() == ECMessage.Direction.SEND) {

                    chatManager.revokeMessage(msg, new ECChatManager.OnRevokeMessageListener() {
                        @Override
                        public void onRevokeMessage(ECError error, ECMessage message) {
                            if (error.errorCode == SdkErrorCode.REQUEST_SUCCESS) {
                                ToastUtil.showMessage("撤回成功");
                                IMessageSqlManager.insertSysMessage("你撤回了一条消息", mRecipients);
                                IMessageSqlManager.delSingleMsg(msg.getMsgId());
                                mChattingAdapter.notifyChange();
                            } else {
                                ToastUtil.showMessage("撤回失败" + error.errorCode);
                            }
                        }
                    });

                }

            } else if ("Copy".endsWith(i)) {

                if (msg.getType() == ECMessage.Type.TXT) {
                    ECTextMessageBody body = (ECTextMessageBody) msg.getBody();
                    ClipboardUtils.copyFromEdit(getActivity(), getString(R.string.app_pic), body.getMessage());
                    ToastUtil.showMessage(R.string.app_copy_ok);
                } else {
                    ECChatManager chatManager = SDKCoreHelper.getECChatManager();
                    if (chatManager == null || msg == null) {
                        return null;
                    }

                    if (msg.getDirection() == ECMessage.Direction.SEND) {
                        chatManager.revokeMessage(msg, new ECChatManager.OnRevokeMessageListener() {
                            @Override
                            public void onRevokeMessage(ECError error, ECMessage message) {
                                if (error.errorCode == SdkErrorCode.REQUEST_SUCCESS) {

                                    ToastUtil.showMessage("撤回成功");
                                    IMessageSqlManager.insertSysMessage("你撤回了一条消息", mRecipients);
                                    IMessageSqlManager.delSingleMsg(msg.getMsgId());
                                    mChattingAdapter.notifyChange();
                                } else {
                                    ToastUtil.showMessage("撤回失败" + error.errorCode);
                                }
                            }
                        });
                    }
                }

            } else if ("查看已读未读".endsWith(i)) {
                ECAlertDialog.buildAlert(getActivity(), "请选择", "已读", "未读", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        Intent action = new Intent();
                        action.setClass(getChattingActivity(), ECMessageFeedUI.class);
                        msg.setSessionId(mRecipients);
                        action.putExtra("type", 1);
                        ECMessageFeedUI.message = msg;
                        startActivity(action);
                    }
                }, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent action = new Intent();
                        action.setClass(getChattingActivity(), ECMessageFeedUI.class);
                        msg.setSessionId(mRecipients);
                        ECMessageFeedUI.message = msg;
                        action.putExtra("type", 2);
                        startActivity(action);
                    }
                }).show();
            } else if ("Favorite".endsWith(i)) {

                Intent intent = new Intent(getActivity(), UploadFileService.class);
                intent.putExtra(UploadFileService.COLLECT_FILE_PATH, fileUrl);
                intent.putExtra(UploadFileService.COLLECT_FILE_TYPE, type);
                intent.putExtra(UploadFileService.COLLECT_SENDER, collectName);
                getActivity().startService(intent);
                fileUrl = "";
            }
        }
        return null;
    }

    /**
     * @param msg
     * @param position
     */
    public void doDelMsgTips(final ECMessage msg, final int position) {
        ECAlertDialog buildAlert = ECAlertDialog.buildAlert(getActivity(), R.string.app_delete_tips, null, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                mHandlerHelper.postRunnOnThead(new Runnable() {
                    @Override
                    public void run() {
                        IMessageSqlManager.delSingleMsg(msg.getMsgId());
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mChattingAdapter.notifyChange();
                            }
                        });
                    }
                });
            }
        });
        buildAlert.setTitle(R.string.chatting_resend_title);
        buildAlert.show();
    }

    public static class SmoothScrollToPosition {
        public static void setSelectionFromTop(ListView listview, int position, int y, boolean smooth) {
            if (listview == null) {
                return;
            }
            LogUtil.i(TAG, "setSelectionFromTop position " + position + " smooth " + smooth);
            listview.setItemChecked(position, true);
            listview.setSelectionFromTop(position, y);
        }

        public static void setSelection(ListView listview, int position, boolean smooth) {
            if (listview == null) {
                return;
            }
            LogUtil.i(TAG, "setSelection position " + position + " smooth " + smooth);
            listview.setItemChecked(position, true);
            listview.setSelection(position);
        }
    }

    /****************************
     * 在线客服
     ****************************/
    @Override
    public void onServiceStart(String event) {
        ToastUtil.showMessage("开启咨询[" + event + "]");
    }

    @Override
    public void onServiceFinish(String even) {
    }

    @Override
    public void onError(ECError error) {
    }
    /****************************在线客服****************************/


    /*******************************************
     * DEBUG START
     *********************************************/
    private void handleTest(final String count) {
        if (TextUtils.isEmpty(count) || count.trim().length() == 0) {
            ToastUtil.showMessage("测试协议失败，测试消息条数必须大于0");
            return;
        }
        final String text = getString(R.string.app_test_message);
        // final String text = getTestText();
        ECAlertDialog buildAlert = ECAlertDialog.buildAlert(getActivity(), "是否开始发送" + count + "条测试消息\n[" + text + "]？", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                mHandlerHelper.postRunnOnThead(new Runnable() {
                    @Override
                    public void run() {
                        debugeTest = true;
                        doStartTest(count, text);
                    }
                });
                mChattingFooter.setEditText("endtest://");
            }
        });
        buildAlert.setTitle("开发模式");
        buildAlert.show();
    }

    private ECMeetingManager.ECCreateMeetingParams mMeetingParams;

    private void handleGroupVoiceCall() {
        ECMeetingManager.ECCreateMeetingParams.Builder builder = new ECMeetingManager.ECCreateMeetingParams.Builder();
        // 设置语音会议房间名称
        builder.setMeetingName("voice")
                // 设置语音会议房间加入密码
                .setMeetingPwd("")
                // 设置语音会议创建者退出是否自动解散会议
                .setIsAutoClose(false)
                // 设置语音会议创建成功是否自动加入
                .setIsAutoJoin(true)
                // 设置语音会议背景音模式
                .setVoiceMod(ECMeetingManager.ECCreateMeetingParams.ToneMode.values()[0])
                // 设置语音会议所有成员退出后是否自动删除会议
                .setIsAutoDelete(true);

        mMeetingParams = builder.create();
        if (mMeetingParams != null) {
            showProcessDialog();
            // 处理创建群聊房间
            MeetingHelper.startVoiceMeeting2(mMeetingParams);
        }
    }

    public void startVoice(String meetingNo) {
        MeetingHelper.queryMeetings(ECMeetingManager.ECMeetingType.MEETING_MULTI_VOICE);
        if (mMeetingParams != null && mMeetingParams.isAutoJoin()) {
            // 自动加入会议的
            ECMeeting meeting = new ECMeeting();
            meeting.setMeetingNo(meetingNo);
            meeting.setMeetingName(mMeetingParams.getMeetingName());
            meeting.setCreator(CCPAppManager.getClientUser().getUserId());
            meeting.setJoined(1);
            doStartMeetingActivity(meeting, null, false);
        }
    }

    /**
     * 跳转到会议聊天界面
     *
     * @param meeting
     * @param pass
     */
    protected void doStartMeetingActivity(ECMeeting meeting, String pass, boolean callin) {
        Intent intent = new Intent(getActivity(), VoiceMeetingActivity.class);
        intent.putExtra(VoiceMeetingActivity.EXTRA_MEETING, meeting);
        if (!TextUtils.isEmpty(pass)) {
            intent.putExtra(VoiceMeetingActivity.EXTRA_MEETING_PASS, pass);
        }
        intent.putExtra(VoiceMeetingActivity.EXTRA_CALL_IN, callin);
        startActivity(intent);
    }

    /**
     * 发送红包消息
     */
    private void handlesendRedPacketMessage(Intent data) {
        String greetings = data.getStringExtra(RedPacketConstant.EXTRA_RED_PACKET_GREETING);
        String moneyID = data.getStringExtra(RedPacketConstant.EXTRA_RED_PACKET_ID);
        String specialReceiveId = data.getStringExtra(RedPacketConstant.EXTRA_RED_PACKET_RECEIVER_ID);
        String redPacketType = data.getStringExtra(RedPacketConstant.EXTRA_RED_PACKET_TYPE);
        String text = "[" + getResources().getString(R.string.ytx_luckymoney) + "]" + greetings;

        com.alibaba.fastjson.JSONObject jsonObject = new com.alibaba.fastjson.JSONObject();
        jsonObject.put(RedPacketConstant.MESSAGE_ATTR_IS_RED_PACKET_MESSAGE, true);//是否是红包消息
        jsonObject.put(RedPacketConstant.EXTRA_SPONSOR_NAME, getResources().getString(R.string.ytx_luckymoney));//红包sponsor name
        jsonObject.put(RedPacketConstant.EXTRA_RED_PACKET_GREETING, greetings);//祝福语
        jsonObject.put(RedPacketConstant.EXTRA_RED_PACKET_ID, moneyID);//红包id
        jsonObject.put(RedPacketConstant.MESSAGE_ATTR_RED_PACKET_TYPE, redPacketType);//红包类型，是否是专属红包
        jsonObject.put(RedPacketConstant.MESSAGE_ATTR_SPECIAL_RECEIVER_ID, specialReceiveId);//指定接收者
        // 组建一个待发送的ECMessage
        ECMessage msg = ECMessage.createECMessage(ECMessage.Type.TXT);
        // 设置消息接收者
        msg.setTo(mRecipients);
        msg.setUserData(jsonObject.toJSONString());
        // 创建一个文本消息体，并添加到消息对象中
        ECTextMessageBody msgBody = new ECTextMessageBody(text.toString());
        msg.setBody(msgBody);
        String[] at = mChattingFooter.getAtSomeBody();
        msgBody.setAtMembers(at);
        mChattingFooter.clearSomeBody();
        try {
            // 发送消息，该函数见上
            long rowId = -1;
            if (mCustomerService) {
                rowId = CustomerServiceHelper.sendMCMessage(msg);
            } else {
                rowId = IMChattingHelper.sendECMessage(msg);
            }
            // 通知列表刷新
            msg.setId(rowId);
            notifyIMessageListView(msg);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 发送礼物消息
     */
    private void handlesendGiftMessage(String json) {

        String text = "[gift]";

        // 组建一个待发送的ECMessage
        ECMessage msg = ECMessage.createECMessage(ECMessage.Type.TXT);
        // 设置消息接收者
        msg.setTo(mRecipients);
        msg.setUserData(json);
        // 创建一个文本消息体，并添加到消息对象中
        ECTextMessageBody msgBody = new ECTextMessageBody(text.toString());
        msg.setBody(msgBody);
        String[] at = mChattingFooter.getAtSomeBody();
        msgBody.setAtMembers(at);
        mChattingFooter.clearSomeBody();
        try {
            // 发送消息，该函数见上
            long rowId = -1;
            if (mCustomerService) {
                rowId = CustomerServiceHelper.sendMCMessage(msg);
            } else {
                rowId = IMChattingHelper.sendECMessage(msg);
            }
            // 通知列表刷新
            msg.setId(rowId);
            notifyIMessageListView(msg);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 发送群聊消息
     */
    public void handlesendConfMessage(String json) {

        String text = "[conf]";

        // 组建一个待发送的ECMessage
        ECMessage msg = ECMessage.createECMessage(ECMessage.Type.TXT);
        // 设置消息接收者
        msg.setTo(mRecipients);
        msg.setUserData(json);
        // 创建一个文本消息体，并添加到消息对象中
        ECTextMessageBody msgBody = new ECTextMessageBody(text.toString());
        msg.setBody(msgBody);
        String[] at = mChattingFooter.getAtSomeBody();
        msgBody.setAtMembers(at);
        mChattingFooter.clearSomeBody();
        try {
            // 发送消息，该函数见上
            long rowId = -1;
            if (mCustomerService) {
                rowId = CustomerServiceHelper.sendMCMessage(msg);
            } else {
                rowId = IMChattingHelper.sendECMessage(msg);
            }
            // 通知列表刷新
            msg.setId(rowId);
            notifyIMessageListView(msg);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 发送话题消息
     */
    private void handlesendTopicMessage(String json) {

        String text = "[topic]";

        // 组建一个待发送的ECMessage
        ECMessage msg = ECMessage.createECMessage(ECMessage.Type.TXT);
        // 设置消息接收者
        msg.setTo(mRecipients);
        msg.setUserData(json);
        // 创建一个文本消息体，并添加到消息对象中
        ECTextMessageBody msgBody = new ECTextMessageBody(text.toString());
        msg.setBody(msgBody);
        String[] at = mChattingFooter.getAtSomeBody();
        msgBody.setAtMembers(at);
        mChattingFooter.clearSomeBody();
        try {
            // 发送消息，该函数见上
            long rowId = -1;
            if (mCustomerService) {
                rowId = CustomerServiceHelper.sendMCMessage(msg);
            } else {
                rowId = IMChattingHelper.sendECMessage(msg);
            }
            // 通知列表刷新
            msg.setId(rowId);
            notifyIMessageListView(msg);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private boolean debugeTest = false;

    private void doStartTest(String count, final String text) {
        try {
            final int num = Integer.parseInt(count);
            ECHandlerHelper handlerHelper = new ECHandlerHelper();
            handlerHelper.postRunnOnThead(new Runnable() {
                @Override
                public void run() {
                    ToastUtil.showMessage("开始测试.");
                    for (int i = 0; i < num && debugeTest; i++) {
                        try {
                            ToastUtil.showMessage("正在发送第[" + (i + 1) + "]条测试消息");
                            final String pretext = "[第" + (i + 1) + "条]\n" + text;
                            getActivity().runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    handleSendTextMessage(pretext);
                                }
                            });
                            Thread.sleep(10);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            mChattingFooter.setEditTextNull();
                            ToastUtil.showMessage("测试结束...");
                        }
                    });
                }
            });
        } catch (Exception e) {
        }
    }

    /*******************************************
     * DEBUG END
     *********************************************/

    public interface OnChattingAttachListener {
        void onChattingAttach();
    }

    private ECProgressDialog mPostingdialog;

    public void showProcessDialog() {
        if (mPostingdialog != null && mPostingdialog.isShowing()) {
            return;
        }
        mPostingdialog = new ECProgressDialog(getActivity(),
                "Waiting");
        mPostingdialog.show();
    }

    /**
     * 关闭对话框
     */
    public void dismissPostingDialog() {
        if (mPostingdialog == null || !mPostingdialog.isShowing()) {
            return;
        }
        mPostingdialog.dismiss();
        mPostingdialog = null;
    }

    public void sendRedPacketAckMessage(String senderId, String senderNickName) {
        com.alibaba.fastjson.JSONObject jsonObject = new com.alibaba.fastjson.JSONObject();
        jsonObject.put(RedPacketConstant.MESSAGE_ATTR_IS_RED_PACKET_ACK_MESSAGE, true);//是否是红包领取消息
        jsonObject.put(RedPacketConstant.EXTRA_RED_PACKET_SENDER_NAME, senderNickName);//发送者昵称
        jsonObject.put(RedPacketConstant.EXTRA_RED_PACKET_SENDER_ID, senderId);//发送者id
        jsonObject.put(RedPacketConstant.EXTRA_RED_PACKET_RECEIVER_NAME, clientUser.getUserName());//接收者昵称
        jsonObject.put(RedPacketConstant.EXTRA_RED_PACKET_RECEIVER_ID, clientUser.getUserId());//接收者id
        String text = getResources().getString(R.string.ytx_luckymoney);
        if (senderId.equals(clientUser.getUserId())) {
            text = this.getResources().getString(R.string.money_msg_take_money);
        } else {
            text = String.format(getResources().getString(R.string.money_msg_take_someone_money), senderNickName);
        }
        // 组建一个待发送的ECMessage
        ECMessage msg = ECMessage.createECMessage(ECMessage.Type.TXT);
        // 设置消息接收者
        msg.setTo(mRecipients);
        msg.setUserData(jsonObject.toJSONString());
        msg.setMsgStatus(ECMessage.MessageStatus.RECEIVE);
        msg.setSessionId(mRecipients);
        // 创建一个文本消息体，并添加到消息对象中
        ECTextMessageBody msgBody = new ECTextMessageBody(text.toString());
        msg.setBody(msgBody);
        String[] at = mChattingFooter.getAtSomeBody();
        msgBody.setAtMembers(at);
        mChattingFooter.clearSomeBody();
        try {
            // 发送消息，该函数见上
            long rowId = -1;
            if (mCustomerService) {
                rowId = CustomerServiceHelper.sendMCMessage(msg);
            } else {
                rowId = IMChattingHelper.sendECMessage(msg);
            }
            // 通知列表刷新
            msg.setId(rowId);
            notifyIMessageListView(msg);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
